我使用ListView
生成了SimpleAdapter
。我的ListView的每一行都有一个名为remove
的按钮。我想要删除或删除在该行以及items
上设置的项目(当我点击Arraylist<Hashmap<String,String>>
按钮时,该项目属于remove
类型。我已经完成了以下编码,但它没有帮助。任何人都可以一步一步指导我做什么?我的代码如下:
//here items is of the type ArrayList<HashMap<String,String>>
ListAdapter k = new SimpleAdapter(cart_activity.this, items,
R.layout.cart_list_view_items, new String[] {"Category","Details","quantity","images","Currency","Price","item_id"}, new int[] { R.id.cat_name_cart,
R.id.cart_details,R.id.cart_qty,R.id.image_url,R.id.cart_currency,R.id.cart_price,R.id.cart_item_id}) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
Log.i("arraylist position in cart activity and position",""+items.get(position).get("item_id")+" "+position);
Button remove=(Button)v.findViewById(R.id.remove);
remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
items.remove(position);
}
});
return v;
}
};
salad_list.setAdapter(k);
答案 0 :(得分:1)
试试这个..
1)从ArrayList
移除.clear()
,因为它会清除ArrayList
2)然后添加将会刷新 ListView 的notifyDataSetChanged()
。
items.remove(position);
k.notifyDataSetChanged();
答案 1 :(得分:0)
点击删除按钮执行:
items.remove(position);
k.notifyDataSetChanged();