2 yongyong 21 yongyong_21 于 2012.09.11 16:19 提问

如何获得listview中的按钮?

在如下程序中,我使用一个List Activity。 执行ArrayAdapter中的getView()方法。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    mPosition= position;
    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]);


    return rowView;

} 

现在我想在deleteButton上设置 onclicklistener()事件。当点击删除键时,相应的内容就会被删除。如何实现?谢谢大家的帮忙

4个回答

curie_87
curie_87   2012.09.11 17:31
已采纳

我是这么解决这个问题的,这是最近在程序中用的一个代码片段:

((Button) l.findViewById(R.id.btn_rm)).setOnClickListener(new OnClickListener(){
public void onClick(View v) { 

    list_of_elements.remove(position);      
    My_Custom_List_Adapter.this.notifyDataSetChanged(); 
}                   
});

please note that this has been developed within an expandable list adapter, so it may differ slightly from a regular listadapter
这是在一个可扩展的列表适配器中使用的代码,可能与一般的列表稍微有些不同。

myc_100
myc_100   2012.09.11 16:48

给getView()里按钮直接添加OnClickListener事件,如下:

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      mPosition= position;
      LayoutInflater inflater = context.getLayoutInflater();
      View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
      deleteButton= (Button)rowView.findViewById(R.id.delete_bn);
      deleteButton.setOnClickListener( new OnClickListener() {
        public void onClick( View v ) {
          //Handle deleting the item here.
          //If you need the layout holding the button, you can probably get it by using v.getParent()
        }
      });
      TextView textView = (TextView) rowView.findViewById(R.id.label);
      textView.setText(names[position]);

      return rowView;
    }
qazwsx132656526
qazwsx132656526   2015.08.31 20:03

给getView()里按钮直接添加OnClickListener事件

u1254z62
u1254z62   2017.01.03 18:15

给getView()里按钮直接添加OnClickListener事件

Csdn user default icon
上传中...
上传图片
插入图片