Android 如何通过单击按钮在listview中获取所选项目
我有一个带有textView的listview,每行中都有一个按钮,我试图通过单击按钮来获取文本,而不是单击整行,但是adapterView方法:(adapterView arg0,View v,int position,long arg3)不适用于按钮单击Android 如何通过单击按钮在listview中获取所选项目,android,listview,button,Android,Listview,Button,我有一个带有textView的listview,每行中都有一个按钮,我试图通过单击按钮来获取文本,而不是单击整行,但是adapterView方法:(adapterView arg0,View v,int position,long arg3)不适用于按钮单击 public View getView(int position, View convertView, ViewGroup parent) { //Set the view for each item in the list
public View getView(int position, View convertView, ViewGroup parent) {
//Set the view for each item in the list view
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.employeeitem, null);
}
//Get the Textviews from the row view and set the appropriate values for them
TextView labelName=(TextView) v.findViewById(R.id.labelName);
TextView labelAddress=(TextView)v.findViewById(R.id.labelAddress);
TextView labelImmat=(TextView)v.findViewById(R.id.labelImmat);
labelName.setText(array[position].marque);
labelAddress.setText(array[position].categorie);
labelImmat.setText(array[position].Prix_Achats);
return v;
}
这是我通过单击listview的行来选择项目的方式,但我希望通过单击按钮而不是整行来选择项目:
listEmployeeDetails.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
{
TextView tv=(TextView)v.findViewById(R.id.labelName);
String name=tv.getText().toString();
Toast.makeText(getApplicationContext(), "Contact Selected "+name, Toast.LENGTH_LONG).show();
}
});
listEmployeeDetails.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView arg0,视图v,内部位置,长arg3)
{
TextView tv=(TextView)v.findViewById(R.id.labelName);
字符串名称=tv.getText().toString();
Toast.makeText(getApplicationContext(),“选定联系人”+姓名,Toast.LENGTH\u LONG.show();
}
});
为您的按钮设置onClickListener:
Button button = (Button) getActivity().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// button was clicked
}
});
我使用游标适配器做了类似的事情 在适配器内部定义onClickListener。在此之前,您必须使用
getTag
和setTag
函数在函数中发送和检索要恢复的信息
myButton.setTag("Value I want to store");
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String value = (String) v.getTag();
//Whatever you want to do
}
});
希望有帮助:)您可以在按钮上设置一个标记,从
getView()
方法访问它。当以编程方式或通过xml声明项时,可以在标记中存储该项的文本
public View getView(int position, View convertView, ViewGroup parent) {
//Set the view for each item in the list view
View v = convertView;
//Do your view stuff here
Button btn = (Button)v.findViewById(R.id.your_button_id);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = (String)v.getTag();
//Do whatever you want with your str here
}
});
return v;
}
listView2.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(getApplicationContext(),“单击”+parent.getItemAtPosition(position).toString(),Toast.LENGTH\u SHORT).show();
}
});
parent.getItemAtPosition(position.toString()
此方法返回该位置的当前选定项通过单击按钮从textview获取文本
btn.setTag(textView.getText().toString());
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s =(String)v.getTag();
}
});
我花了三天时间才解决这个问题,但解决办法是显而易见的。我的列表是使用
JSONArray
从SQL
服务器扩展而来的,在getView()
方法中,我要做的就是在使用jsonOjbect扩展后,将TextView
传递到按钮的setTag()
。然后在按钮的onClick()
方法中,我必须获得标签。看一下代码,希望对我有所帮助
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListCell cell;//ListCell is my widgets class holder
//set up the convertView
if(convertView==null){
convertView=inflater.inflate(R.layout.menu_custom_layout, null);
cell=new ListCell();
cell.Diet=(TextView)convertView.findViewById(R.id.dietEat); //Diet is the custom list layout textview
cell.eatButton=(Button)convertView.findViewById(R.id.eatButtton); //eatButton is the button to be clicked
cell.eatButton.setOnClickListener(this);
}else{
cell=(ListCell)convertView.getTag();
}
//Change data of each cell
try {
JSONObject jsonObject=this.dataArray.getJSONObject(position);
cell.Diet.setText(jsonObject.getString("food_name"));
cell.eatButton.setTag(cell.Diet.getText()); //Tag for button's onclick Listener
} catch (JSONException e) {
e.printStackTrace();
}
return convertView;
}
@Override
public void onClick(View v) {
String fud=(String) v.getTag(); //Getting the tag and parsing it to string
}
发布适配器getView代码。这不是我的问题,我的问题是如何通过单击按钮获取listview行中的文本
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListCell cell;//ListCell is my widgets class holder
//set up the convertView
if(convertView==null){
convertView=inflater.inflate(R.layout.menu_custom_layout, null);
cell=new ListCell();
cell.Diet=(TextView)convertView.findViewById(R.id.dietEat); //Diet is the custom list layout textview
cell.eatButton=(Button)convertView.findViewById(R.id.eatButtton); //eatButton is the button to be clicked
cell.eatButton.setOnClickListener(this);
}else{
cell=(ListCell)convertView.getTag();
}
//Change data of each cell
try {
JSONObject jsonObject=this.dataArray.getJSONObject(position);
cell.Diet.setText(jsonObject.getString("food_name"));
cell.eatButton.setTag(cell.Diet.getText()); //Tag for button's onclick Listener
} catch (JSONException e) {
e.printStackTrace();
}
return convertView;
}
@Override
public void onClick(View v) {
String fud=(String) v.getTag(); //Getting the tag and parsing it to string
}