充气按钮点击监听器不起作用

Inflater button click listener not work

我有一个自定义列表视图,我想给 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;

} }

所以在此 BaseAdapter 中,我想为 holder.btnStatus 提供监听器,但在列表中,监听器只处理一行,我想动态地为监听器提供。 请仔细检查我的代码并向我提出一些解决方案。

改变这个

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

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

编辑2

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

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