0

There are multiple items in listview . I am confused on how to add selected listview holder items into hashmap and then send Arraylist of Hashmap type to another cart activity. Here is my code:

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;
    }

}

My question is: what should I apply in holder.add button clicklistener so that selected position will be added into hashmap?

lebelinoz
  • 4,890
  • 10
  • 33
  • 56
curious
  • 9
  • 3

1 Answers1

1

What you can do is simply add data to the object of ShoppingData class rather hashmap and then pass this object to another activity read this example it will teach you how to pass Object from one activity to another.
EDIT:
of you can create hash map like Map<String , ShoppingData> will be easy to add send and delete!

phpdroid
  • 1,642
  • 1
  • 18
  • 36