Reputation: 61
i am doing simple cart application. which will show cart details in recycler view. Here i am going to decrease the quantity value upto 1.The item going to be remove from item adapter.it will be working fine.But when i was going to decrease the second item quantity value,it will remove without checking the condition. My condition is if the quantity is below 1 the item going to remove.
ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
Context context;
ArrayList<ItemData> itemDataList;
int quantity,t_amount;
totalAmount totalAmount;
public ItemAdapter(Context context, ArrayList<ItemData> itemDataList) {
this.context = context;
this.itemDataList = itemDataList;
totalAmount = (ItemAdapter.totalAmount) context;
}
@NonNull
@Override
public ItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_details,parent,false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int pos) {
ItemData itemData = itemDataList.get(pos);
holder.txtPrice.setText(itemData.getPrice());
holder.txtDesc.setText(itemData.getDescription());
holder.txtCartCount.setText(itemData.getCartCount());
holder.btnCartDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(quantity <= 1){
itemDataList.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(),itemDataList.size());
}else {
quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
quantity = quantity-1;
itemDataList.get(pos).setCartCount(String.valueOf(quantity));
notifyDataSetChanged();
holder.txtCartCount.setText(String.valueOf(quantity));
}
}
});
holder.btnCartInc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
quantity = quantity+1;
itemDataList.get(pos).setCartCount(String.valueOf(quantity));
notifyDataSetChanged();
holder.txtCartCount.setText(String.valueOf(quantity));
}
});
}
@Override
public int getItemCount() {
return itemDataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtPrice, txtDesc, txtCartCount, btnCartDec, btnCartInc;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtPrice = itemView.findViewById(R.id.itemView_price);
txtDesc = itemView.findViewById(R.id.itemView_desc);
txtCartCount = itemView.findViewById(R.id.itemView_Count);
btnCartDec = itemView.findViewById(R.id.itemViewDec);
btnCartInc = itemView.findViewById(R.id.itemViewInc);
}
}
public interface totalAmount{
void t_amount(int amount);
}
}
Upvotes: 4
Views: 6372
Reputation: 116
You can remove from list of that position and notify adater ,it will remove.
replace this code
itemDataList.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(),itemDataList.size());
to this
itemDataList.remove(holder.getAdapterPosition());notifyDataSetChanged();
it will work
Upvotes: 0
Reputation: 492
you can remove your item from model at specific position.After that notify Adapter that some changes has been done in list using notifyDataSetChanged();
.
list.remove(getAdapterPosition())
notifyDataSetChanged();
Upvotes: 0
Reputation: 3128
You must get the quantity
related to the position before your if condition. In your case the quantity
value is the previous value, that's why the if
condition is true.
holder.btnCartDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// You must get the quantity of the currently clicked item here
quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
if(quantity <= 1){
itemDataList.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(),itemDataList.size());
}else {
quantity = Integer.parseInt(itemDataList.get(pos).getCartCount());
quantity = quantity-1;
itemDataList.get(pos).setCartCount(String.valueOf(quantity));
notifyDataSetChanged();
holder.txtCartCount.setText(String.valueOf(quantity));
}
}
});
Upvotes: 0
Reputation: 583
Just need knows the item position and adds below code in adapter
mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataSet.size());
Upvotes: 5