如何从ArrayList <hashmap <string,string>&gt;中删除值从android中的ListView行?</hashmap <string,string>

时间:2014-07-16 09:34:53

标签: android android-listview arraylist hashmap simpleadapter

我使用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);

2 个答案:

答案 0 :(得分:1)

试试这个..

1)ArrayList移除.clear(),因为它会清除ArrayList

中的所有详细信息

2)然后添加将会刷新 ListView notifyDataSetChanged()

items.remove(position);
k.notifyDataSetChanged();

答案 1 :(得分:0)

点击删除按钮执行:

items.remove(position);
k.notifyDataSetChanged();