在我的应用程序中,我使用List活动,其中我已经传递了一个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()。当单击相应的删除按钮时,它应该删除该项目。请帮帮我。
答案 0 :(得分:2)
您可以使用可用于视图的setTag方法。在deletebutton setTag和onClicklistener上设置id / position你可以通过view.getTag()获取id / position
答案 1 :(得分:1)
您应该将OnClickListener
直接添加到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.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;
}
答案 2 :(得分:1)
我最近在我的应用程序中使用了一段代码
((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();
}
});
请注意,这是在expandablelistadapter中开发的,因此它可能与常规listadapter略有不同
答案 3 :(得分:0)
1.使用方法delete(int position)创建接口。
2.在您使用ListView的活动中实施它
delete(int position)
{
adapter.remove(position) // modify the syntax as per need .
}
3.通过构造函数将此接口传递给适配器。
4.在getView for deletebutton中编写onClickListener并调用i nterface.delete(position)
答案 4 :(得分:0)
...
deleteButton.setOnClickListener( new OnClickListener() {
public void onClick( View v ) {
//Handle deleting the item here.
// or item enable -> false
v.getParent()
}
});
...