【问题标题】:How to get row view data in the listview如何在列表视图中获取行视图数据
【发布时间】:2011-09-21 14:30:13
【问题描述】:

这个例子:http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html。custom_row_view.xml 中有 3 个 textview id。使用 onListItemClick 时,一个位置列表项内有 3 行数据。如何提取这些数据?身份证怎么用?无论如何,单击列表项时在列表视图中获取行视图数据 [protected void onListItemClick(ListView l, View v, int position, long id]?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    参数View v本身就是行视图。您可以拨打v.getTag()获取附件数据。应该使用v.setTag(Object)在适配器的getView中更早地设置它

    【讨论】:

    • 这个例子:saigeethamn.blogspot.com/2010/04/…。 custom_row_view.xml 中有 3 个文本视图 ID。使用 onListItemClick 时,一个位置列表项内有 3 行数据。如何提取这些数据? id怎么用?
    • 您可以使用findviewbyId 并获取附加到视图的标签。
    【解决方案2】:

    取决于数据类型。例如,id 确实包含使用 CursorAdapter 之一设置的数据库表行中的 _id。通常这是该数据库表行的 PK。

    【讨论】:

      【解决方案3】:
      listView.getAdapter().getItemAt(position)
      

      获取绑定到视图 V 的对象

      【讨论】:

        【解决方案4】:

        我建议你不要从视图中获取数据,而是使用你用来设置数据到ListView的Adapter的ArrayList。 p>

        在您指出的示例中,您使用的是 HashMap 的 ArrayList。举个例子..

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // arrayList is the variable which you have used as a list in your SimpleAdapter
                hashMap = arrayList.get((int)id); // you need to typecast 'id' from long to int
                value = hashMap.get(KEY);
            }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-16
          相关资源
          最近更新 更多