I am use recycle view to show a list of item from room database. the recycle view is reside in a fragment, when I navigate to fragment for the first time the items appear duplicated twice, but when I click in a button inside item, all items appear at once as desired. this is my adapter this is my adapter.
private Context context;
private List<Product> productList;
private BroojDB database;
public CartAdapter(Context context, List<Product> productList) {
this.context = context;
this.productList = productList;
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cart_recycle_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Product product = productList.get(position);
int count = product.getCount();
float productPrice = product.getPrice();
float subTotal = count*productPrice;
database = BroojDB.getInstance(context);
holder.count.setText(product.getCount()+"");
holder.title.setText(product.getTitle());
holder.price.setText(product.getPrice()+" SD");
holder.subTotal.setText(subTotal+" SD");
holder.deleteProductFromCart.setBackgroundResource(R.drawable.cancel);
holder.increaseOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Product p = productList.get(holder.getAdapterPosition());
int pId = p.getId();
String pTitle = p.getTitle();
float pPrice = p.getPrice();
int pCount = p.getCount();
pCount++;
database.productDao().updateProduct(pId,pTitle,pPrice,pCount);
productList.clear();
productList.addAll(database.productDao().getAllProduct());
notifyDataSetChanged();
}
});
holder.decreaseOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Product p = productList.get(holder.getAdapterPosition());
int pId = p.getId();
String pTitle = p.getTitle();
float pPrice = p.getPrice();
int pCount = p.getCount();
if (pCount >=1)
pCount = pCount - 1;
database.productDao().updateProduct(pId,pTitle,pPrice,pCount);
productList.clear();
productList.addAll(database.productDao().getAllProduct());
notifyDataSetChanged();
}
});
holder.deleteProductFromCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Product p = productList.get(holder.getAdapterPosition());
database.productDao().deleteProduct(p);
int position = holder.getAdapterPosition();
productList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,productList.size());
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Button increaseOne,decreaseOne,deleteProductFromCart;
TextView title, price,subTotal,count;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
increaseOne = itemView.findViewById(R.id.increase_product_one);
decreaseOne = itemView.findViewById(R.id.decrease_one);
deleteProductFromCart = itemView.findViewById(R.id.remove_item);
title = itemView.findViewById(R.id.item_title);
price = itemView.findViewById(R.id.item_price);
subTotal = itemView.findViewById(R.id.sub_total);
count = itemView.findViewById(R.id.count_off_product_in_cart);
}
}