1

I'm working on a POS management system where user can choose an item from a product list and add the selected values in a check list. I can successfully add a new item to the check list after clicking on the product list, but can't update a check list item's quantity which is already added before. See the image for a clear demonstration,

Screenshot

From the image you can see that each product item was added as a new item in check list instead of updating the quantity.

I'm looking for a solution to update the check list quantity after clicking on a item from the product list.

Here's my on click listener for the product list recyclerview,

recyclerProducts.addOnItemTouchListener(
                new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        // TODO Handle item click

                        // productList is the arraylist used in product list 
                        Products product = productList.get(position);

                        Contents c = new Contents();
                        c.setTitle(product.getName());
                        c.setQuantity(product.getQuantity());
                        orderList.add(c);
                        recyclerListAdapter.notifyDataSetChanged();

                    }
                })
        );

Here's the adapter for check list,

private List<Contents> contentsList;

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public TextView quantity;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.txtTitle);
            quantity = (TextView) view.findViewById(R.id.txtQuantity);
        }
    }

    public RecyclerListAdapter(List<Contents> contentsList) {
        this.contentsList = contentsList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_product, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Contents contents = contentsList.get(position);

        holder.title.setText(contents.getTitle());
        holder.quantity.setText("1");
    }

    @Override
    public int getItemCount() {
        return contentsList.size();
    }
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50

2 Answers2

1

Should do something like this instead

for(Contents contents : orderList){
    if (contents.getTitle().equals(product.getName())){
        contents.setQuantity(product.getQuantity())
        recyclerListAdapter.notifyDataSetChanged();
        return;
    }
//assume existing item was not found and add as before
Contents c = new Contents();
c.setTitle(product.getName());
c.setQuantity(product.getQuantity());
orderList.add(c);
recyclerListAdapter.notifyDataSetChanged();
return;

Using a hashmap as a container instead of a list would be a better option since I assume product types/names are unique.

Declaration of map.

Map<String,Contents> contentsMap = new HashMap<>();

Usage

    Contents contents = contentsMap.get(product.getName());
    if (contents != null){
        contents.setQuantity(product.getQuantity());//update existing quantity
        recyclerListAdapter.notifyDataSetChanged();
    } else{
        Contents newContents = new Contents();
        newContents.setTitle(product.getName());
        newContents.setQuantity(product.getQuantity());
        contentsMap.put(product.getName(),newContents);
        recyclerListAdapter.notifyDataSetChanged();
    }

Notice a hash map does not require iterating over a bunch of values to update the quantity. When you need to iterate over the values, you can use the map.values() method like so.

for (Contents contents: contentsMap.values()){
     //doSomething with contents, ie. create a view
}
alan7678
  • 2,223
  • 17
  • 29
  • Would you mind giving an example of using the Hashmap? – user2582686 Apr 15 '16 at 14:23
  • One last question, How I'm going to get the single hashmap item for setting the textview in onBindViewHolder() method? Previously I received the items via, Contents contents = contentsList.get(position); holder.title.setText(contents.getTitle()); @alan7678 – user2582686 Apr 16 '16 at 05:52
  • A couple of ways, one is to keep a list of the keys(product names) and retrieve the data like so map.get(keyList.get(position)). Another way would be to convert the map to a list using the values() method. – alan7678 Apr 17 '16 at 21:44
  • please answer this related question: https://stackoverflow.com/q/46690220/6144372 – Hammad Nasir Oct 12 '17 at 01:06
0

I am thinking what you want to do here is in your cart (container that holds your items), you want to know item id and since you already know the item id in your original list, you can check every time you add a new item if it already exists and if so, update it instead of adding a new one.

I am assuming you are storing the data (for each order) in a local table with columns. So you just have to query for that particular item and update if it already exists.

Just my two cents and I hope it helps!

Good luck and happy coding!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46