Android - 获取 ListView 项目的 id [特殊情况]

发布于 2024-11-19 19:44:09 字数 1658 浏览 3 评论 0原文

在我的数据库类中有一个返回游标的方法。光标将包含来自不同表的数据:

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 caCursor 附加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╰◇生如夏花灿烂 2024-11-26 19:44:09

在 onItemClick 方法中尝试这个...

final SimpleCursorAdapter simpleCursorAdapter = (SimpleCursorAdapter) parent.getAdapter();
final Cursor cursor = simpleCursorAdapter .getCursor();

final int idColIndex = cursor.getColumnIndex(KEY_ROWID); //_id column of your table
final int rowId = cursor.getInt(idColIndex);

在我的一个项目中使用的是 链接引用第 41 行。希望有所帮助。

编辑:

已解决。见评论。

In onItemClick method try this...

final SimpleCursorAdapter simpleCursorAdapter = (SimpleCursorAdapter) parent.getAdapter();
final Cursor cursor = simpleCursorAdapter .getCursor();

final int idColIndex = cursor.getColumnIndex(KEY_ROWID); //_id column of your table
final int rowId = cursor.getInt(idColIndex);

Used in one of my project here is link refer line number 41. Hope this help.

Edit:

Solved. See comments.

初吻给了烟 2024-11-26 19:44:09

您想要位置参数而不是 id

请参阅 onItemClick 的文档:

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id) 

自:API 级别 1 单击此 AdapterView 中的项目时将调用回调方法。

如果实施者需要访问与所选项目关联的数据,则可以调用 getItemAtPosition(position)。

参数

parent:单击发生的 AdapterView。

view:被单击的 AdapterView 中的视图(这将是适配器提供的视图)

位置:视图在适配器中的位置。

id:被单击的项目的行 ID。

You want the position parameter not the id

See the documentation of onItemClick:

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id) 

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

parent: The AdapterView where the click happened.

view: The view within the AdapterView that was clicked (this will be a view provided by the adapter)

position: The position of the view in the adapter.

id: The row id of the item that was clicked.

南渊 2024-11-26 19:44:09

您可以在 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.

一紙繁鸢 2024-11-26 19:44:09

您可以使用 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()

來不及說愛妳 2024-11-26 19:44:09

我找到了解决问题的方法。

虽然它不是专业的解决方案,但它是我现在唯一的解决方案。我在每个表中添加了一个名为: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文