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

问题描述:

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

参数View v本身就是行视图。通过调用v.getTag()获取附加数据应该使用适配器的getView早期设置v.setTag(Object)

+0

这个例子:http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html。 custom_row_view.xml上有3个textview ID。使用onListItemClick时,一个位置listitem内有3行数据。如何提取这些数据?如何使用ID? – conanlive

+1

您可以使用'findviewbyId'并获取附加到视图的标签。 – Ronnie

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

listView.getAdapter().getItemAt(position) 

让你绑定到视图V

我想建议你不要从视图获取数据的对象,而不是使用您所使用的数据集到ListView的适配器ArrayList中。

在你指出的例子中,你使用了一个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); 
    } 
});