2010-06-18 34 views
5

有人可以解释游标是如何工作的吗?或者以下部分代码的流程?我知道这是子活动,除了我不明白Cursor是如何工作的。有人可以解释在android游标吗?

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(); 
    } 
}); 

谢谢。

回答

3

游标就像从数据库资源创建的列表/指针。 (在PHP这样想通过请求mysql_query()A $水库)

当您运行

managedQuery(data, null, null, null, null); 

您查询联系人,它返回的游标在结果记录指针

然后你从这个Cursor创建一个适配器。适配器是从源获取的结果的对象级别表示,这次是游标,也就是数据库中的记录。 (在PHP中,适配器像Smarty模板阵列一样,阵列是适配器)

如果您知道基于事件的编程,setOnItemClickListener应该很容易理解。

相关问题