我是 Android 开发的新手……来自 iPhone 和 .Net 背景。我见过与这个问题非常相似的问题,但没有一个涉及 SimpleCursorAdapter。
我有一个基本的 ListActivity,它使用 Cursor 将 SQLite 查询中的数据绑定到我的 ListView:
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
c,
new String[] {"name"},
new int[] {android.R.id.text1});
setListAdapter(adapter);
然后当一个项目被点击时:
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Difference between this:
Cursor c = (cursor)l.getItemAtPosition(position);
//and this??
Cursor c = (Cursor)l.getAdapter().getItem(position);
int categoryId = c.getInt(0);
}
这是获取所选元素的 id 的正确方法吗?这似乎很奇怪,因为我认为在数据库关闭后(即在我绑定之后)我不能使用我的光标。当我似乎无法找到从该 id 获取实际项目的方法时,传入的 id 有什么意义?另外,我不明白为什么 getItemAtPosition() 会返回一个游标......游标绑定到整个列表;不只是一排。最后,如果这是正确的,那么显示的两种获取光标的方式有区别吗?谢谢。