如何将选定的listview持有者项添加到hashmap中

时间:2017-09-17 17:37:51

标签: android listview arraylist hashmap android-viewholder

listview中有多个项目。我很困惑如何将选定的listview持有者项添加到hashmap中,然后将Hashmap类型的Arraylist发送到另一个购物车活动。这是我的代码:

Shopping_list.java

class ListViewAdapter extends BaseAdapter {
    Context mContext;
    LayoutInflater inflater;
    private List<ShoppingData> datalist = null;
    private ArrayList<ShoppingData> arraylist;




    public ListViewAdapter(Context context, List<ShoppingData> worldpopulationlist) {
        mContext = context;

        this.datalist = worldpopulationlist;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<ShoppingData>();
        this.arraylist.addAll(worldpopulationlist);
    }

    public class ViewHolder {
        ImageView icon;
        TextView title,price;
        EditText quantity;
        Button add,remove;

    }

    @Override
    public int getCount() {
        return datalist.size();

    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

         imgUrl="http://www.domainname.com/images/Product/medium/";
        final ViewHolder holder;

        holder = new ViewHolder();


        view=inflater.inflate(R.layout.shopping_list_item,null);

        holder.icon=(ImageView)view.findViewById(R.id.image_shooping);
        holder.title=(TextView)view.findViewById(R.id.text_shopping);
        holder.price=(TextView)view.findViewById(R.id.price_shopping);
        holder.quantity=(EditText)view.findViewById(R.id.item_cart);
        holder.add=(Button)view.findViewById(R.id.add_cart);
        holder.remove=(Button)view.findViewById(R.id.remove_item);


        holder.title.setText(datalist.get(position).getName());
        holder.price.setText("Price :"+datalist.get(position).getAmt()+" Rs");

        Glide
                .with(mContext)
                .load(imgUrl+datalist.get(position).getImg())
                .into(holder.icon);

        holder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for(int i=0;i<contents.size();i++)
                {
                    HashMap<String,String>map=new HashMap<String, String>(i);
                    map.remove("name");
                    map.remove("Amount");
                    map.remove("image");
                    map.remove("quantity");
                    map.remove("total");
                    map.remove("tax");
                    map.remove("discount");
                    map.remove("productId");
                    map.remove("bv");
                    map.remove("pv");

                    contents.set(i,map);
                    contents.remove(map);

                    Toast.makeText(Shopping_list.this, datalist.get(position).getName() + "Succesfully Removed ", Toast.LENGTH_SHORT).show();


                    holder.add.setVisibility(View.VISIBLE);
                    holder.remove.setVisibility(View.INVISIBLE);
                    holder.quantity.setText("");
                    holder.add.setText("Add to cart");

                }
            }
        });


        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                holder.quantity.setVisibility(View.VISIBLE);


                        if(holder.quantity.getText().toString().compareTo("")==0)
                {

                    Toast.makeText(Shopping_list.this, " Enter Valid Quantity", Toast.LENGTH_SHORT).show();

                }
                else {
                            for (int i = 0; i < datalist.size() ; i++) {
                                HashMap<String, String> map = new HashMap<String, String>(i);

                                float netAmount = Float.parseFloat(datalist.get(position).getAmt()) * Float.parseFloat(holder.quantity.getText().toString());
                                String net = String.valueOf(netAmount);

                                map.put("name", datalist.get(position).getName());
                                map.put("Amount", datalist.get(position).getAmt());
                                map.put("image", imgUrl + datalist.get(position).getImg());
                                map.put("quantity", holder.quantity.getText().toString());
                                map.put("total", net);
                                map.put("tax",datalist.get(position).getTax());
                                map.put("discount",datalist.get(position).getDiscount());
                                map.put("productId",datalist.get(position).getProductId());
                                map.put("bv",datalist.get(position).getBv());
                                map.put("pv",datalist.get(position).getPv());

                                Toast.makeText(Shopping_list.this, datalist.get(position).getName() + " Succesfully Added to cart", Toast.LENGTH_SHORT).show();

                                holder.add.setText("Added");
                                holder.remove.setVisibility(View.VISIBLE);

                                contents.add(map);
                                System.out.println("haspmap" + contents);
                                System.out.println("contents size"+contents.size());



                            }


                        }
            }
        });

        return view;
    }

}

public class ShoppingData {
    private String name;

    private String amt;
    private String img;
    private String tax;
    private String discount;
    private String productId;
    private String bv;
    private String pv;


    public ShoppingData(String nm,  String amt ,String img,String tax,String discount,String productId,String bv,String pv) {
        this.name = nm;

        this.amt = amt;

        this.img=img;
        this.tax=tax;
        this.discount=discount;
        this.productId=productId;
        this.bv=bv;
        this.pv=pv;

    }

    public String getName() {
        return this.name;
    }
    public String getAmt() {
        return this.amt;
    }
    public String getImg()
    {
        return this.img;
    }
    public String getTax()
    {
        return this.tax;
    }

    public String getDiscount()
    {
        return  this.discount;
    }

    public String getProductId()
    {
        return this.productId;
    }
    public String getBv()
    {
        return this.bv;
    }
    public String getPv()
    {
        return this.pv;
    }

}

我的问题是:我应该在holder.add按钮clicklistener中应用什么,以便将所选位置添加到hashmap中?

1 个答案:

答案 0 :(得分:1)

您可以做的只是将数据添加到ShoppingData类的对象而非hashmap,然后将此对象传递给另一个活动阅读this example它将教您如何传递Object一项活动到另一项活动 修改 你可以创建像Map<String , ShoppingData>这样的哈希地图,很容易添加发送和删除!