
在上篇博客,Android-ListView-SimpleAdapter,中介绍了SimpleAdapter的使用操作(SimpleAdapter面向的数据是非Cursor数据),而SimpleCursorAdapter面向的数据是(Cursor数据)
SimpleCursorAdapter 也是 BaseAdapter的子类,SimpleCursorAdapter内部实现是继承了BaseAdapter,对Cursor进行了逻辑包装等操作
布局代码:
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ll_et_id"
android:layout_marginTop="20dp"></ListView>
Java代码:
/**
* 查询
*/
public void query(View view) {
cursor = contentResolver.query(uri,
new String[]{"_id", "name", "age"},
null, null
, null, null); /**
* 使用SimpleCursorAdapter 适配器
*/
SimpleCursorAdapter simpleCursorAdapter = new
SimpleCursorAdapter(MainActivity.this, // 上下文
R.layout.layout_item, // Item布局
cursor, // Cursor 查询出来的游标 这里面有数据库里面的数据
new String[]{"_id", "name", "age"}, // 从哪里来,指的是 查询出数据库列名
new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age}, // 到哪里去,指的是,把查询出来的数据,赋值给Item布局 的控件
SimpleCursorAdapter.NO_SELECTION); // 设置ListView适配器
listview.setAdapter(simpleCursorAdapter); }