在我的数据库类中有一个返回游标的方法。游标将包含来自不同表的数据:
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);
}
然后,我使用 aSimpleCursorAdapter ca
附加Cursor
到 a 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 !任何解决方案或想法?