我有一个包含两列category_id
和name
的类别表。我创建了一个名为CategoryDataHelper
的数据帮助类。我有一个名为getCategoryCursor()
的方法,它从类别表中提取id和名字并返回游标。使用该光标,我使用SimpleCursorAdapter
来显示类别列表。它工作正常。如何在onItemClick处理程序中获取物品ID
public class Categories extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categoryDataHelper = new CategoryDataHelper(getApplicationContext());
Cursor categoryCursor = categoryDataHelper.getCategoryCursor();
ListAdapter adapter = new SimpleCursorAdapter (
this,
android.R.layout.simple_list_item_1,
categoryCursor,
new String[] { CategoryDataHelper.NAME },
new int[] {android.R.id.text1});
// Bind to our new adapter.
setListAdapter(adapter);
list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Here I want the category_id
}
});
}
}
现在我想实现一个OnItemClickListener
与所选类别的category_id
发送意图。我如何获得onItemClick()
方法中的ID?
是不是ID参数类型长帮助你? – 2011-04-05 14:09:45
要获取所选项目的内容,请使用 Object o = lv.getItemAtPosition(position); 对象o可以进一步用于获取项目。 – 2011-04-05 14:11:06
setListAdapter是做什么的,它为什么会出现在list = getListView()之前? – 2011-04-05 14:13:51