我的 android 列表 Activity 中有一个 ListView ,它通过 json 文件获取数据。在 ListView 中,我必须显示项目的基本详细信息。当我们点击一个项目时,我需要显示有关所点击项目的更多信息。那么如何识别用户点击了哪个项目呢?
最佳答案
在您的监听器实现中,您引用了被单击的 View
,如果是 itemClick
(或 ExpandableList: groupClick
/childClick
) 您还可以在列表中找到基础数据的位置。
因此,只需使用该位置从您用于填充列表(适配器)的数据结构中检索点击的数据。
更新
如果您有一个 ListActivity
,其中 dataList
成员保存您在列表中显示的值,您只需覆盖它的 onListItemClick
方法:
/**
* The list of custom data you display in this activity
*/
private ArrayList<MyData> dataList;
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final MyData selectedValue = dataList.get(position);
//TODO: deal with this selectedValue
}
onListItemClick
的
API 文档
This method will be called when an item in the list is selected. Subclasses should override.
Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
参数:
l
点击所在的ListView 发生了v
被点击的 View ListViewposition
View 的位置 在列表中id
项目的行 ID 点击
您可以在 Android-er here 上找到完整的示例和 here .
关于android - 获取android列表中被点击的item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5853800/