首页 文章

如何在列表视图中获取行视图数据

提问于
浏览
1

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

4 回答

  • 0

    参数 View v 本身是行视图 . 您可以通过调用 v.getTag() 来获取附加数据 . 应该使用 v.setTag(Object) 在适配器的getView中设置它

  • 0

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

  • 0
    listView.getAdapter().getItemAt(position)
    

    获取绑定到视图V的对象

  • 5

    我建议你不要从视图中获取数据,而是 use the ArrayList you have used to set data to the Adapter of the ListView .

    在您指出的示例中,您使用的是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);
        }
    });
    

相关问题