listview与sqlite数据绑定
时间:2021-07-01 10:21:17
帮助过:3人阅读
[java] view plaincopy
- import java.util.ArrayList;
- import java.util.HashMap;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.ContentValues;
- import android.content.DialogInterface;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.View;
- import android.view.View.OnCreateContextMenuListener;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
-
- public class ListView_SqliteActivity extends Activity {
-
- SQLiteDatabase mDb;
- SQLiteDatabaseDao dao;
-
- ArrayList<HashMap<String, Object>> listData;
-
- SimpleAdapter listItemAdapter;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- dao = new SQLiteDatabaseDao();
-
- ListView list = (ListView) findViewById(R.id.list_items);
- listItemAdapter = new SimpleAdapter(ListView_SqliteActivity.this,
- listData,
- R.layout.item,
-
- new String[] { "image", "username", "birthday" },
-
- new int[] { R.id.image, R.id.username, R.id.birthday });
- list.setAdapter(listItemAdapter);
- list.setOnCreateContextMenuListener(listviewLongPress);
- }
-
-
-
- class SQLiteDatabaseDao {
-
- public SQLiteDatabaseDao() {
- mDb = openOrCreateDatabase("users.db",
- SQLiteDatabase.CREATE_IF_NECESSARY, null);
-
- createTable(mDb, "student");
-
- insert(mDb, "student");
-
- getAllData("student");
- }
-
-
- public void createTable(SQLiteDatabase mDb, String table) {
- try {
- mDb.execSQL("create table if not exists "
- + table
- + " (id integer primary key autoincrement, "
- + "username text not null, birthday text not null,image text);");
- } catch (SQLException e) {
- Toast.makeText(getApplicationContext(), "数据表创建失败",
- Toast.LENGTH_LONG).show();
- }
- }
-
-
- public void insert(SQLiteDatabase mDb, String table) {
-
-
- ContentValues values = new ContentValues();
- values.put("username", "LiMei");
- values.put("birthday", "Birthday:6-18");
- values.put("image", R.drawable.o);
- mDb.insert(table, null, values);
-
- values.put("username", "LinQiao");
- values.put("birthday", "Birthday:8-22");
- values.put("image", R.drawable.t);
- mDb.insert(table, null, values);
-
- values.put("username", "WiLee");
- values.put("birthday", "Birthday:9-12");
- values.put("image", R.drawable.f);
- mDb.insert(table, null, values);
-
- }
-
-
- public void getAllData(String table) {
- Cursor c = mDb.rawQuery("select * from " + table, null);
- int columnsSize = c.getColumnCount();
- listData = new ArrayList<HashMap<String, Object>>();
-
- while (c.moveToNext()) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- for (int i = 0; i < columnsSize; i++) {
- map.put("id", c.getString(0));
- map.put("username", c.getString(1));
- map.put("birthday", c.getString(2));
- map.put("image", c.getString(3));
- }
- listData.add(map);
- }
- }
-
-
- public boolean delete(SQLiteDatabase mDb, String table, int id) {
- String whereClause = "id=?";
- String[] whereArgs = new String[] { String.valueOf(id) };
- try {
- mDb.delete(table, whereClause, whereArgs);
- } catch (SQLException e) {
- Toast.makeText(getApplicationContext(), "删除数据库失败",
- Toast.LENGTH_LONG).show();
- return false;
- }
- return true;
- }
- }
-
-
- OnCreateContextMenuListener listviewLongPress = new OnCreateContextMenuListener() {
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
-
- final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
- new AlertDialog.Builder(ListView_SqliteActivity.this)
-
- .setTitle("删除当前数据")
-
- .setIcon(android.R.drawable.ic_dialog_info)
-
- .setMessage("确定删除当前记录")
- .setPositiveButton("是",
- new DialogInterface.OnClickListener() {
- public void onClick(
- DialogInterface dialoginterface, int i) {
-
- int mListPos = info.position;
-
- HashMap<String, Object> map = listData
- .get(mListPos);
-
- int id = Integer.valueOf((map.get("id")
- .toString()));
-
- if (dao.delete(mDb, "student", id)) {
-
- listData.remove(mListPos);
- listItemAdapter.notifyDataSetChanged();
- }
- }
- })
- .setNegativeButton("否",
- new DialogInterface.OnClickListener() {
- public void onClick(
- DialogInterface dialoginterface, int i) {
-
-
- }
- }).show();
- }
- };
-
- @Override
- public void finish() {
-
- super.finish();
- mDb.close();
- }
- }
2.xml文件
main.xml
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <ListView
- android:id="@+id/list_items"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:divider="#ffffff"
- android:dividerHeight="1dip" />
-
- </RelativeLayout>
item.xml
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RelativeLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip"
- android:paddingRight="12dip" >
-
- <ImageView
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:paddingLeft="6dip"
- android:paddingTop="6dip" />
-
- <TextView
- android:id="@+id/username"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingTop="6dip"
- android:textColor="#ccc"
- android:textSize="18dip" />
-
- <TextView
- android:id="@+id/birthday"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/username"
- android:maxLines="2"
- android:paddingRight="20dip"
- android:textColor="#fff" />
-
- </RelativeLayout>
listview与sqlite数据绑定
标签: