依照惯例我们先来看下谷歌官方文档对游标适配器的介绍,该适配器可以将数据库的数据暴露给ListView,将数据显示在LIstView空间上。特别注意的一点是,官方文档上写道游标必须包含一个列命名为“_id”,否则这个类将不会工作。此外,使用与这个类MergeCursor不会工作如果合并后的游标有重叠的的“_id”列中的值。 通过对官网文档的解读,我们就可以知道通过游标适配器访问数据库库的表单的数据,一定要创建一列名为"_id",否则就会报错。现在lets,go!
MainAcitivity的代码
package com.example.f13_cursor02; import com.example.f13_cursor.dao.DBManager; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.support.v4.widget.SimpleCursorAdapter; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ListView; public class MainActivity extends Activity { private Button button; private ListView listView; private SimpleCursorAdapter adapter;//生命一个游标适配器 private DBManager dbManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbManager = new DBManager(this); listView = (ListView) this.findViewById(R.id.listView1); button = (Button) this.findViewById(R.id.button1); Cursor cursor = dbManager.query("person", null, null, null, null, null, null, null); adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[] { "name", "address" }, new int[] { R.id.textView1, R.id.textView2 }); listView.setAdapter(adapter); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub ContentValues contentValues=new ContentValues(); contentValues.put("name", "jack"); contentValues.put("address", "hangzhou"); dbManager.insert("person", null, contentValues); } }); adapter.notifyDataSetChanged(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
MyOpenHelper类的代码,该类继承SQLiteOpenHelper,用于创建数据库。
package com.example.f13_cursor.dao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String PERSON_TABLE_NAME = "person"; public MyOpenHelper(Context context) { super(context, PERSON_TABLE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("create table " + PERSON_TABLE_NAME + "(_id integer primary key autoincrement,name varchar(64),address varchar(64))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }DBManager类,用于对数据库进行增删改查
package com.example.f13_cursor.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class DBManager { private MyOpenHelper helper; private SQLiteDatabase database; public DBManager(Context context) { // TODO Auto-generated constructor stub helper = new MyOpenHelper(context); database = helper.getWritableDatabase(); } public boolean insert(String table, String nullColumnHack, ContentValues values) { boolean flag = false; long count = database.insert(table, nullColumnHack, values); flag = count > 0 ? true : false; return flag; } public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) { Cursor cursor = null; cursor = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy); return cursor; } public void conn() { if (database != null) { database.close(); } } }