CursorAdpater
继承与BaseAdpter是一个虚类,需自行实现里面的方法。
为ListView和Cursor提供了连接的桥梁.
Makes a new view to hold the data pointed to by cursor.
找到ListView的Item组件,可以使用ViewHolder
abstract View newView(Context context, Cursor cursor, ViewGroup parent)
将ViewHolder中的View和Cursor中的数据绑定起来
Bind an existing view to the data pointed to by cursor
abstract void bindView(View view, Context context, Cursor cursor)
注意cursor的必须要有个命名为”_id”的列。比如Contacts._ID就为”_id”
下面通过一个简单的例子演示CursorAdapter的用法
这个Demo特别简单,从数据库中获取数据,然后通过ListView显示
数据库帮助类
public class MyDBHelper extends SQLiteOpenHelper {
private static String DB_NAME = "TESTDB";
private static int VERSION = 1;
public MyDBHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE person(_id INTEGER PRIMARY "
+ "KEY AUTOINCREMENT,name VARCHAR(10),age INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
}
}
MainActivity
public class MainActivity extends ActionBarActivity {
private ListView mList;
private CursorAdapter mAdapter;
private Cursor mCursor;
private MyDBHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = (ListView) findViewById(R.id.id_lv_person);
mDBHelper = new MyDBHelper(this);
SQLiteDatabase database = mDBHelper.getReadableDatabase();
mCursor = database.rawQuery("SELECT * FROM person", null);
makeCursorAdapter();
mList.setAdapter(mAdapter);
}
/**
* 初始化CursorAdapter
*/
private void makeCursorAdapter() {
mAdapter = new CursorAdapter(this, mCursor) {
// 初次创建View时,找到布局和控件
@Override
public View newView(Context context, Cursor cursor, ViewGroup arg2) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_list, null);
ViewHolder holder = new ViewHolder();
holder.name = (TextView) view.findViewById(R.id.id_tv_name);
holder.age = (TextView) view.findViewById(R.id.id_tv_age);
view.setTag(holder);
return view;
}
// 绑定View时,复用布局
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
holder.name.setText(name);
holder.age.setText(age + "");
}
// 在匿名内部类声明内部类需使用final修饰
final class ViewHolder {
TextView name;
TextView age;
}
};
}
// 插入测试数据
public void insertData(View view) {
SQLiteDatabase db = mDBHelper.getWritableDatabase();
db.execSQL("insert into person(name, age) values('xxx',21)");
db.close();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下面将介绍CursorAdapter和CursorLoader混合使用,实现异步加载数据。