【问题标题】:Android - Getting the id of a ListView item [Special Case]Android - 获取 ListView 项的 id [特例]
【发布时间】: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 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(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


    【解决方案1】:

    在 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);
    

    在我的一个项目中使用的是 link 参考第 41 行。希望对您有所帮助。

    编辑:

    已解决。见 cmets。

    【讨论】:

    • 这将为我提供所选项目的id,不知道它属于哪个表。我想知道which table the item belong to,以便我可以正确检索它。谢谢@gopal
    • 哦,明白了,很抱歉没有正确理解您的问题。 Neway,我认为很难找出哪个表 id 属于,因为......当你最后进行联合时,你会得到一堆标题和 fav 作为列的行。当您对每个 _id 具有不同的值时,我只能看到一种方法让您确定它属于哪个表,我的意思是某种模式。对于 ex table1 键以 T1 开头...table2 键以 T2 开头...依此类推...一些模式来区分。希望对您有所帮助。
    • 好主意。它对我有很大帮助。我解决了这个问题。谢谢@gopal :)
    【解决方案2】:

    你想要的是位置参数而不是 id

    查看onItemClick的文档:

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

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

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

    参数

    parent:发生点击的 AdapterView。

    view:AdapterView中被点击的view(这将是adapter提供的view)

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

    id:被点击项的行id。

    【讨论】:

      【解决方案3】:

      您可以在 MsgView 类中创建一个静态数据成员,用于存储 id,或者您可以向该类发送延迟消息,确保在消息到达之前加载并启动活动。

      【讨论】:

        【解决方案4】:

        您可以使用View.setTag()View.getTag() 方法在视图中存储任何数据。有时我利用它来为 ListView 中的每一行“标记”一个 Intent 对象。每当我需要查找一行的数据时,我只需使用 getTag() 获取 Intent

        【讨论】:

          【解决方案5】:

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

          虽然不是专业的解决方案,但它是我现在唯一的一个。我在每个表中添加了一个名为:tableNumber 的新列,以便每个表都有不同的编号。然后,我可以根据该数字区分项目。

          感谢您的回答。特别感谢@gopal

          【讨论】:

          • 您还可以使用自定义投影图指定表,其中一些静态值被别名为预期的虚拟列名,UNION 中的每个查询将为别名指定自己的值列名;类似:"SELECT 'table1' AS type, rowid AS _id, title, fav FROM table1 UNION ALL SELECT 'table2' AS type, rowid AS _id, title, fav FROM table2 ..."。然后您可以通过调用String type = cursor.getString(cursor.getColumnIndex("type")); 来确定该项目来自哪个表
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-02
          • 1970-01-01
          • 1970-01-01
          • 2011-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多