Reputation: 875
I am trying to update the shopping cart with just the quantity if the quantity in the cart is already 1 but it just will not work. My cart array will not update.
Here is my viewHolder class where the array menuItemArrayList consists of the products and the array selectionItemArrayList is the shopping cart. The int totalAmount which is the $ sum of products in the cart is being calculated just fine.
public class DishViewHolder extends RecyclerView.ViewHolder{
public TextView tvMenuItemName, tvMenuItemDescription, tvMenuItemPrice;
public ImageView ivMenuItemImage;
public Button btnInitialAdd;
public ArrayList<SelectionItem> selectionItemArrayList = new ArrayList<>();
public DishViewHolder(@NonNull View itemView) {
super(itemView);
tvMenuItemName = itemView.findViewById(R.id.menu_item_name);
tvMenuItemDescription = itemView.findViewById(R.id.menu_item_description);
ivMenuItemImage = itemView.findViewById(R.id.image_menu_item);
tvMenuItemPrice = itemView.findViewById(R.id.menu_item_price);
btnInitialAdd = itemView.findViewById(R.id.button_initial_add);
btnInitialAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
for (SelectionItem selectionNameCheck: selectionItemArrayList) {
if (selectionNameCheck.equals(menuItemArrayList)) {
selectionNameCheck.selectionQuantity++;
} else {
SelectionItem selectionItem = new SelectionItem(menuItemArrayList.get(position).getMenuItemName(), menuItemArrayList.get(position).getMenuItemPrice(), 1);
selectionItemArrayList.add(selectionItem);
}
}
totalAmount += Integer.parseInt(tvMenuItemPrice.getText().toString());
amountAddListener.onAmountAdd(totalAmount, selectionItemArrayList);
}
});
In the for each block I am traversing through the cart array to see if the product has already been entered. If yes then just update the quantity otherwise add a new array element which is the chosen product.
Upvotes: 0
Views: 51
Reputation: 733
Equals doesn't work like that.Instead of
if (selectionNameCheck.equals(menuItemArrayList)) {
selectionNameCheck.selectionQuantity++;
}
Use:
if (menuItemArrayList.contains(selectionNameCheck)){
selectionNameCheck.selectionQuantity++;
}
Upvotes: 2