点击ListView的孩子时,得到的ListView数据 [英] Get data from ListView when clicking ListView's child

查看:137
本文介绍了点击ListView的孩子时,得到的ListView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每行一个按钮,一个ListView。如果我需要得到该行被点击时的数据,那么这将是pretty容易做到的onItemClickListener内的以下内容:

I have a ListView with one button per row. If I needed to get the data when the row gets clicked, then it would be pretty easy to do the following inside the onItemClickListener:

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            CustomType type = (CustomType) arg0.getItemAtPosition(arg2);        //get data related to position arg2 item
        }
    });

其实,事情是,我需要获得数据(即:CustomType对象)时,ListView的行的按钮获得的点击,而不是行本身。由于OnClickListener没有像适配器视图作为
参数(显然),我想知道我怎么能解决这个?
到目前为止,途中发生对我越来越按钮的母公司,是至极列表视图,并在至极行的位置点击的按钮在某种程度上得到,然后
打电话是这样的:
myAdapter.getItem(位置);
但只是一个想法,所以请,我将AP preciate一些帮助在这里。

Actually, the thing is that I need to get the data(i.e: CustomType object) when the button of the ListView's row get's clicked, and not the row itself. Since OnClickListener doesn't have something like AdapterView as a parameter(obviously), I would like to know how can I handle this?. So far, It ocurred to me getting the button's parent, wich is the listview, and somehow getting in wich row's position the clicked button is, and then call something like: myAdapter.getItem(position); but is just an idea, so please, I'll appreciate some help over here.

先谢谢了。

推荐答案

您可能正在使用自定义适配器的的ListView 所以最简单的方法做你想做的在适配器的 getView()方法来设置位置参数作为<$ C $标签C>按钮。然后,您可以检索标记中的 OnClickListener 键,然后你就会知道被点击哪一行:

You're probably using a custom adapter for your ListView so the easiest way to do what you want is to set in the getView() method of the adapter the position parameter as the tag for the Button. You could then retrieve the tag in the OnClickListener and you'll know then which row was clicked:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //...
    button.setTag(Integer.valueOf(position));
    button.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
              Integer rowPosition = (Integer)v.getTag();
         }
    });
    //...
}

您也可以提取该行视图中的数据。这会工作,如果所有的行的数据可以从该行的视图中找到:

You could also extract the data from the row views. This will work if all the data for the row can be found in the view from that row:

button.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
          LinearLayout row = (LinearLayout)v.getParent(); I assumed your row root is a LinearLayout
         // now look for the row views in the row and extract the data from each one to
         // build the entire row's data 
     }
});

这篇关于点击ListView的孩子时,得到的ListView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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