Home » Android » android – Having challenge in RecyclerView item's onClickListener-Exceptionshub

android – Having challenge in RecyclerView item's onClickListener-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have an item list which are placed under a RecyclerView. On any item, there a quantity(add or subtract). When add is selected, the quantity should increase one.

enter image description here

But when i clicked on ‘+’ for the first time, the value of quantity is setting to 1 and soon after resetting to 0, but when i click on second time the value is becoming ‘1’. Similarly when i click on next time the value is setting to 2 and after a fraction of second it is still getting to 1 and i click on another time the value is becoming 2. Why by first click is not getting recognized? Can someone please help me here?

public class ItemListAdapter  extends RecyclerView.Adapter<ItemListAdapter.ItemViewHolder>{

    private Context mContext;
    private List<Items> itemsList;
    private OnItemListListner onClick;

    public ItemListAdapter(Context mContext,List<Items> itemsList)
    {
        this.mContext=mContext;
        this.itemsList=itemsList;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(mContext);
        View view=inflater.inflate(R.layout.item_details_layout,null);
        return new ItemListAdapter.ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemViewHolder holder, final int position) {
        Items items=itemsList.get(position);
        Log.d("onBindViewHolder",items.getItemName());
        holder.itemNameTV.setText(items.getItemName());
        String strItemPrice=mContext.getResources().getString(R.string.Rs)+" "+String.valueOf(items.getItemPrice());
        holder.itemPriceTV.setText(strItemPrice);
        holder.itemDescTV.setText(items.getItemDescription());

        Glide.with(mContext).load(items.getItemImagePath()).into(holder.imageView);

        holder.itemAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int intCurrentQuantity=Integer.valueOf(holder.itemQuantity.getText().toString());
                intCurrentQuantity+=1;
                Log.d("myQuantity",String.valueOf(intCurrentQuantity));
                holder.itemQuantity.setText(String.valueOf(intCurrentQuantity));
                notifyItemChanged(holder.getAdapterPosition());
            }
        });


        holder.itemNameTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onItemListClick(position);
            }
        });
        holder.itemPriceTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onItemListClick(position);
            }
        });
        holder.itemDescTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onItemListClick(position);
            }
        });
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onItemListClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return itemsList.size();
    }

    public void SetOnClick(OnItemListListner onClick)
    {
        this.onClick=onClick;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView itemNameTV,itemPriceTV,itemDescTV,itemQuantity,itemSubtract,itemAdd;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView=itemView.findViewById(R.id.itemImageView);
            itemNameTV=itemView.findViewById(R.id.itemNameTextView);
            itemPriceTV=itemView.findViewById(R.id.itemPriceTextView);
            itemDescTV=itemView.findViewById(R.id.itemDescritionTextView);
            itemQuantity=itemView.findViewById(R.id.itemQuantity);
            itemSubtract=itemView.findViewById(R.id.subtractQuantity);
            itemAdd=itemView.findViewById(R.id.addQuantity);
        }
    }

    public interface OnItemListListner{
        void onItemListClick(int position);
    }

}
How to&Answers:

Recycler view recycles the views on scroll and resets its content, so you have to persist the quantity qty variable. You can do it by two ways

  1. Store quantity inside your Items model.
  2. Persist quantity in seperate HashMap as item.getId() as key and qty as value.

then get and set from the model object or hashmap.

Answer:

Reviewd your code and then after i’m just agreed with Harsh’s Answer
there is a two way to set your adapter. most commonly used Model for storing and retriving data.

You need to create a one model class and and set data first into the model then after get data from model and set into your adapter / notify your adapter.

You need to create one variable in model class like below snippet:

public class Item {
    String mQty = "";
    public String getmQty() {
        return mQty;
    }

    public void setmQty(String mQty) {
        this.mQty = mQty;
    }
}

Helpful Answe: Click Me

Answer:

Check updated code

 holder.itemAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int intCurrentQuantity=Integer.valueOf(holder.itemQuantity.getText().toString());
                ++intCurrentQuantity; //use this    
                Log.d("myQuantity",String.valueOf(intCurrentQuantity));
                holder.itemQuantity.setText(String.valueOf(intCurrentQuantity));
                notifyItemChanged(holder.getAdapterPosition());
            }
        });

Answer:

you should have the quantity property in Item model class and have to follow the code I added below.

holder.itemAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int quantity = item[getAdapterPosition()].quantity;
quantity = quantity +1;
item[getAdapterPosition()].setQuantity(quantity);
notifyItemChanged(getAdapterPosition());
        }
    });

Thanks.I am sure this will work for you.