Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击ListView时从ListView获取数据';孩子_Android_Android Layout_Android Listview - Fatal编程技术网

Android 单击ListView时从ListView获取数据';孩子

Android 单击ListView时从ListView获取数据';孩子,android,android-layout,android-listview,Android,Android Layout,Android Listview,我有一个每行一个按钮的列表视图。如果我需要在单击行时获取数据,那么在侦听器中执行以下操作将非常容易: @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { CustomType type = (CustomType) arg0.getItemAtPosition(arg2);

我有一个每行一个按钮的列表视图。如果我需要在单击行时获取数据,那么在侦听器中执行以下操作将非常容易:

        @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
        }
    });
@覆盖
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
CustomType type=(CustomType)arg0.getItemAtPosition(arg2);//获取与位置arg2项相关的数据
}
});
实际上,我需要在单击ListView的行get的按钮时获取数据(即:CustomType对象),而不是该行本身。因为OnClickListener没有像
AdapterView
这样的东西作为 参数(显然),我想知道如何处理这个

到目前为止,我想到了获取按钮的父级,也就是listview,然后以某种方式获取单击按钮所在行的位置,然后 称之为:
myAdapter.getItem(位置)

但这只是一个想法,因此,请允许我在此提供一些帮助。

在适配器中添加一个自定义方法,该方法返回
CustomType
对象

    public CustomType  getObjectDetails(int clickedPosition){
        CustomType  customType = this.list.get(clickedPosition);
        return customType ;
    }


 public void onItemClick(AdapterView<?> arg0, View arg1, int Position,long arg3) {
          CustomType type = getObjectDetails(Position);
     }
    });
public CustomType getObjectDetails(int-clickedPosition){
CustomType CustomType=this.list.get(单击位置);
返回自定义类型;
}
public void onItemClick(适配器视图arg0、视图arg1、内部位置、长arg3){
CustomType类型=getObjectDetails(位置);
}
});

在适配器中添加一个返回
CustomType
对象的自定义方法

    public CustomType  getObjectDetails(int clickedPosition){
        CustomType  customType = this.list.get(clickedPosition);
        return customType ;
    }


 public void onItemClick(AdapterView<?> arg0, View arg1, int Position,long arg3) {
          CustomType type = getObjectDetails(Position);
     }
    });
public CustomType getObjectDetails(int-clickedPosition){
CustomType CustomType=this.list.get(单击位置);
返回自定义类型;
}
public void onItemClick(适配器视图arg0、视图arg1、内部位置、长arg3){
CustomType类型=getObjectDetails(位置);
}
});

您可能正在为您的
列表视图使用自定义适配器,因此最简单的方法是在适配器的
getView()
方法中设置
position
参数作为
按钮的标记。然后,您可以在
OnClickListener
中检索标记,然后您将知道单击了哪一行:

@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();
         }
    });
    //...
}
您还可以从行视图中提取数据。如果可以在该行的视图中找到该行的所有数据,则此操作将起作用:

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
使用自定义适配器,因此,最简单的方法是在适配器的
getView()
方法中设置
position
参数作为
按钮的标记。然后,您可以在
OnClickListener
中检索标记,然后您将知道单击了哪一行:

@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();
         }
    });
    //...
}
您还可以从行视图中提取数据。如果可以在该行的视图中找到该行的所有数据,则此操作将起作用:

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 
     }
});

我需要在按钮的click事件监听器内部执行类似操作,而不是ListView的onItemClick监听器我需要在按钮的click事件监听器内部执行类似操作,而不是ListView的onItemClick监听器