【发布时间】:2012-03-13 22:01:18
【问题描述】:
在我的数据库类中有一个返回游标的方法。游标将包含来自不同表的数据:
public Cursor fetchFavTitles() {
return myDataBase.rawQuery("SELECT rowid as _id, title, fav FROM table1 " +
"WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table2 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table3 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table4 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table5 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table6 WHERE fav = 1", null);
}
然后,我使用SimpleCursorAdapter ca 将Cursor 附加到ListView lv:
Cursor cursor = myDbHelper.fetchTitles(c);
String[] columns = {cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.item_title};
ca = new SimpleCursorAdapter(this.getBaseContext(), R.layout.items_layout, cursor,columns , columnsLayouts);
lv = getListView();
lv.setAdapter(ca);
现在,如果我选择 ListView 中的任何项目,我希望获得对该项目 ID 的引用。 我希望 id 从项目所属的表中检索其他信息。并在其他活动 MsgView 中检索它
我试过这个:
有对点击项目时传递的 id 的引用
-
通过意图传递该值,以便其他活动将使用此传递的值检索所需的数据。
lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(view.getContext(), MsgView.class); i.putExtra("id", (int) id); // startActivity(i); } });
但是,这不起作用。看来我使用的 id 不是项目的 id ! 有什么解决方案或想法吗?
【问题讨论】:
标签: android sqlite android-listview simplecursoradapter android-cursor