有人能解释一下光标的具体工作原理吗?或者下面部分代码的流程?我知道这是子 Activity ,但我不明白 Cursor 是如何工作的。
final Uri data = Uri.parse("content://contacts/people/");
final Cursor c = managedQuery(data, null, null, null, null);
String[] from = new String[] { People.NAME };
int[] to = new int[] { R.id.itemTextView };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.listitemlayout, c, from, to);
ListView lv = (ListView) findViewById(R.id.contactListView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
c.moveToPosition(pos);
int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));
Uri outURI = Uri.parse(data.toString() + rowId);
Intent outData = new Intent();
outData.setData(outURI);
setResult(Activity.RESULT_OK, outData);
finish();
}
});
谢谢。
最佳答案
游标就像从数据库资源创建的列表/指针。 (在 PHP 中,想像来自 mysql_query() 的 $res)
当你运行时
managedQuery(data, null, null, null, null);
您查询联系人,它返回一个 Cursor,它是结果中记录的指针
然后您从这个 Cursor 创建一个适配器。适配器是从源中获取的结果的对象级表示,这次是游标,也就是数据库中的记录。 (在 PHP 中,适配器就像 Smarty 模板的数组一样,数组就是适配器)
如果您了解基于事件的编程,setOnItemClickListener 应该很容易理解。
关于android - 有人可以解释一下 android 中的 Cursor 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3069412/