获取android系统列表中单击项 [英] Get the clicked item in android list

查看:123
本文介绍了获取android系统列表中单击项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid名单的活动,它通过JSON文件中获取数据的列表视图。在列表视图中我必须证明该项目的基本细节。当我们点击一​​个项目,我需要显示在单击的项目的更多信息。那么,如何确定哪些项目用户点击?

I have a list view in my android list activity which get data through a json file. In the list view I have to show the basic details of the item. When we click on a item I need to show more information on the item clicked. So how can I identify which item user has clicked?

推荐答案

在你的监听器实现你必须参照查看单击的,并且在的情况下, itemClick在(或ExpandableList:群组相关 / childClick ),您也有列表里面的底层数据的位置。

In your listener implementation you have reference to the View that was clicked, and in case of itemClick (or ExpandableList: groupClick / childClick) you also have the position of the underlying data inside your list.

因此​​,只要使用的位置从数据结构检索数据点击您用来填充列表(适配器)。

So just use the position to retrieve the clicked data from the data structure you used to populate the list (adapter).

更新

如果你有一个 ListActivity ,其中的DataList 成员保存您在列表中显示的值,你只是覆盖它的 onListItemClick 方法:

If you have a ListActivity, in which the dataList member holds the values you are displaying in the list, you just override it's onListItemClick method:

/**
 * 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
}

API文档 onListItemClick

这个方法将被调用时
  列表中的项目被选择。
  子类应该重写。

  子类可以调用
  getListView()。getItemAtPosition(位置)
  如果他们需要访问数据
  与所选择的项目相关联

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.

参数


  • ListView控件,其中点击
    发生

  • v 被点击中的视图
    在ListView

  • 位置视图的位置
    在列表中

  • ID 这是该项目的行ID
    点击

  • l The ListView where the click happened
  • v The view that was clicked within the ListView
  • position The position of the view in the list
  • id The row id of the item that was clicked

您可以在的Andr​​oid-ER在这里找到一个完整的例子这里

这篇关于获取android系统列表中单击项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆