我有一个包含两列category_id
和的类别表name
。我创建了一个名为CategoryDataHelper
. 我有一个名为getCategoryCursor()
该助手类的方法,它从类别表中获取 id 和名称并返回游标。使用该光标,我习惯于SimpleCursorAdapter
显示类别列表。它工作正常。
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
所选类别的 Intent。如何在onItemClick()
方法中获取 id?