如何在ListView内的Button上设置setOnclickListener()?

时间:2011-11-14 12:19:37

标签: android button android-listview listactivity

在app中我有一个Listactivity,它有一个带TextView和Button的适配器(标记为delete)。现在我想删除相应的Button点击项目。 请检查代码并建议???? `

public class MySimpleArrayAdapter extends ArrayAdapter<String> implements OnClickListener {
    private final Activity context;
    private final String[] names;
    private Button deleteButton= null;
    public MySimpleArrayAdapter(Activity context, String[] names) {
        super (context, R.layout.imagelistlayout, names);
        this.context = context;
        this.names = names;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
        deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
        deleteButton.setTag(position);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(names[position]);
        deleteButton.setOnClickListener(this); 
        return rowView;

    }

    @Override
    public void onClick(View convertView) {
        System.out.println(deleteButton.getTag());

    }

}` 

我想知道如何删除已点击按钮的项目。

4 个答案:

答案 0 :(得分:9)

您应该尝试不在getView方法中对onClick处理程序进行硬编码,而是查看如何将onClick方法分配给listview。在这里,您可以从活动中分配方法,这也是您应该在此处执行的操作。

在你的适配器中创建一个名为setOnXXXClickListener

的方法
public void setOnXXXClickListener(final OnClickListener onClickListener) {
    this.onXXXClickListener = onClickListener;
}

并在你的getView中将此分配给按钮,如此

viewHolder.xxx.setOnClickListener(this.onXXXClickListener);

从你的Activity中你可以像这样分配onClick方法

this.adapter.setOnXXXClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "OnXXXClickListener");
    }
});

答案 1 :(得分:8)

只需处理getview中的点击监听器,您可以使用findviewbyid找到该按钮

这将处理当前行按钮单击

public class MySimpleArrayAdapter extends ArrayAdapter<String> implements OnClickListener {
    private final Activity context;
    private final String[] names;
    private Button deleteButton= null;
    public MySimpleArrayAdapter(Activity context, String[] names) {
        super (context, R.layout.imagelistlayout, names);
        this.context = context;
        this.names = names;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
        deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
        deleteButton.setTag(position);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(names[position]);
       deleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //try to hide textview or something it may help
            }
        });
        return rowView;

    }

}`

答案 2 :(得分:0)

deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Log.d("TAG", "position: " + position);
        }
    });

你需要在getView(final int position,...,...)中进行最终定位 当你拥有这个职位时,随心所欲!删除,修改或其他..

答案 3 :(得分:0)

很简单,你在getView中有位置int变量,它指的是按钮膨胀的项目,所以使用位置变量来删除你点击的项目。只需使用以下代码。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
        deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
        deleteButton.setTag(position);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(names[position]);
       // deleteButton.setOnClickListener(this); 
deleteButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //use position here
                            // delete the item from data string havng position index and the use notifydataset
            }

        });
        return rowView;

    }