Android - 获取 ListView 项目的 id [特殊情况]
在我的数据库类中有一个返回游标的方法。光标将包含来自不同表的数据:
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(AdapterViewparent, View view, int 位置,长 id) { Intent i = new Intent(view.getContext(), MsgView.class); i.putExtra("id", (int) id); // 开始活动(一); } });
然而,这并没有奏效。看来我使用的 id 不是该项目的 id ! 有什么解决方案或想法吗?
In my DB class have a method that returns a Cursor. the cursor will contain data from different tables:
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);
}
Then, I use a SimpleCursorAdapter ca
to attach the Cursor
to 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);
Now, if I select any item in the ListView, I want to have a reference to that item id.
I want the id to retrieve other information from the table the item belong to. And retrieve it in other activity MsgView
I tried this:
have a reference to the id passed when an item clicked
pass that value with the intent, so that the other activity will retrieve the needed data using this passed value.
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); } });
However, this didn't work. It seems that the id I'm using is not the item's id !
Any solution or ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 onItemClick 方法中尝试这个...
在我的一个项目中使用的是 链接引用第 41 行。希望有所帮助。
编辑:
已解决。见评论。
In onItemClick method try this...
Used in one of my project here is link refer line number 41. Hope this help.
Edit:
Solved. See comments.
您想要位置参数而不是 id
请参阅 onItemClick 的文档:
自:API 级别 1 单击此 AdapterView 中的项目时将调用回调方法。
如果实施者需要访问与所选项目关联的数据,则可以调用 getItemAtPosition(position)。
参数
You want the position parameter not the id
See the documentation of onItemClick:
Since: API Level 1 Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
您可以在
MsgView
类中创建一个静态数据成员,用于存储 id,或者您可以向该类发送延迟消息,确保活动在消息到达之前加载并启动。You could create a static data member in your
MsgView
class that you use to store the id, or you can send a delayed message to the class, making sure that the activity gets loaded and started before the message arrives.您可以使用 View.setTag() 和 View.getTag() 存储任何数据的方法在你看来。有时我利用它来将 Intent 对象“标记”到 ListView 中的每一行。每当我需要查找一行数据时,我只需使用 getTag() 返回 Intent
You can use the View.setTag() and View.getTag() methods to store any data in your views. Sometimes I have exploited this to 'tag' a Intent object to each row in a ListView. Whenever I need to lookup data for a row i just get the Intent back with getTag()
我找到了解决问题的方法。
虽然它不是专业的解决方案,但它是我现在唯一的解决方案。我在每个表中添加了一个名为:
tableNumber
的新列,以便每个表都有不同的编号。然后,我可以根据该数字区分项目。感谢您的回答。特别感谢@gopal
I found a solution to my problem.
Although it is not a professional solution, but it is the only one I have now. I added a new column in each table named:
tableNumber
so that each table will have a different number. Then, I can distinguish between items based on that number.Thanks for your answers. and special thanks to @gopal