i have one custom listview and i want to give click listener into the Button ui object but it's not working, please kindly go through my below code and suggest me some solution.
我有一个自定义列表视图,我想给点击监听器进入Button ui对象,但它不起作用,请通过我的下面的代码,并建议我一些解决方案。
package com.example.dynamicbutton;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Activity activity;
ArrayList<String> name;
ViewHolder holder;
public CustomAdapter(Activity activity, ArrayList<String> name) {
this.activity = activity;
this.name = name;
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int position) {
return name.get(position);
}
@Override
public long getItemId(int position) {
return name.size();
}
public class ViewHolder {
TextView fruitName;
Button btnStatus;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.activity_custom_layout, parent, false);
holder = new ViewHolder();
holder.fruitName = (TextView)convertView.findViewById(R.id.fruit_name);
holder.btnStatus = (Button)convertView.findViewById(R.id.btn_status);
holder.btnStatus.setText("Like");
holder.fruitName.setText(name.get(position));
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.btnStatus.getText().toString().equals("Like")) {
holder.btnStatus.setText("Unlike");
}else{
holder.btnStatus.setText("Like");
}
}
});
return convertView;
} }
So here in this BaseAdapter i want to give listener to holder.btnStatus but in the list the listener is working on only one row i want to give the listener dynamically. Please kindly go through my code and suggest me some solution.
所以在这个BaseAdapter中我想给监听器持有者.btnStatus但是在列表中,监听器只在一行上工作,我想动态地给监听器。请仔细阅读我的代码并建议我一些解决方案。
1 个解决方案
#1
Change this
LayoutInflater mInflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.activity_custom_layout, parent, false);
holder = new ViewHolder();
holder.fruitName = (TextView)convertView.findViewById(R.id.fruit_name);
holder.btnStatus = (Button)convertView.findViewById(R.id.btn_status);
holder.btnStatus.setText("Like");
holder.fruitName.setText(name.get(position));
To
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.activity_custom_layout, parent, false);
holder = new ViewHolder();
holder.fruitName = (TextView)convertView.findViewById(R.id.fruit_name);
holder.btnStatus = (Button)convertView.findViewById(R.id.btn_status);
holder.btnStatus.setText("Like");
holder.fruitName.setText(name.get(position));
convertView.setTag(holder);
}
else holder = (ViewHolder) convertView.getTag();
EDIT2
holder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.btnStatus.getText().toString().equals("Like")) {
holder.btnStatus.setText("Unlike");
}else{
holder.btnStatus.setText("Like");
}
}
});
to
final ViewHolder holder_fin = holder;
holder_fin.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder_fin.btnStatus.getText().toString().equals("Like")) {
holder_fin.btnStatus.setText("Unlike");
}else{
holder_fin.btnStatus.setText("Like");
}
}
});
#1
Change this
LayoutInflater mInflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.activity_custom_layout, parent, false);
holder = new ViewHolder();
holder.fruitName = (TextView)convertView.findViewById(R.id.fruit_name);
holder.btnStatus = (Button)convertView.findViewById(R.id.btn_status);
holder.btnStatus.setText("Like");
holder.fruitName.setText(name.get(position));
To
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.activity_custom_layout, parent, false);
holder = new ViewHolder();
holder.fruitName = (TextView)convertView.findViewById(R.id.fruit_name);
holder.btnStatus = (Button)convertView.findViewById(R.id.btn_status);
holder.btnStatus.setText("Like");
holder.fruitName.setText(name.get(position));
convertView.setTag(holder);
}
else holder = (ViewHolder) convertView.getTag();
EDIT2
holder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.btnStatus.getText().toString().equals("Like")) {
holder.btnStatus.setText("Unlike");
}else{
holder.btnStatus.setText("Like");
}
}
});
to
final ViewHolder holder_fin = holder;
holder_fin.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder_fin.btnStatus.getText().toString().equals("Like")) {
holder_fin.btnStatus.setText("Unlike");
}else{
holder_fin.btnStatus.setText("Like");
}
}
});