Inflater按钮单击侦听器不起作用

时间:2015-05-13 10:30:11

标签: android listview button onclicklistener baseadapter

我有一个自定义列表视图,我想给Click 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中我想给监听器持有者.btnStatus但是在列表中,监听器只在一行上工作,我想动态地给监听器。 请仔细阅读我的代码,并向我提出一些解决方案。

1 个答案:

答案 0 :(得分:0)

更改此

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

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

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