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监听器