【发布时间】:2011-04-05 14:06:14
【问题描述】:
我有一个包含两列 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?
【问题讨论】:
-
long类型的id参数不是对你有帮助吗??
-
获取选中项目的内容,使用 Object o=lv.getItemAtPosition(position);对象 o 可以进一步用于获取项目。
-
setListAdapter 是做什么的,为什么会出现在 list = getListView() 之前?
标签: android onclick onclicklistener simpleadapter