I am making a food order app. There is a full page ListView
on my main page called MenuActivity
and there is a Button
when I press it, it will intent to the CartActivity
page.
I have arrays food
for food name, imgID
for food images, butPlus
butMinus
to adjust the amount of food and amount
to keep the amount of each food with an initial value of 0.
I have a method called addAmount
, minusAmount
in MenuAcitivity
that will increase, decrease the amount. It is called by an Adapter
of this class.
public static void addAmount(int position) {
amount[position] += 1;
}
Going to CartActivity
, I used this to pass all my values.
public void goToCart(MenuItem menuItem) {
Intent in = new Intent(MenuActivity.this, CartActivity.class);
in.putExtra("list", amount);
in.putExtra("imgID", imgID);
in.putExtra("nameList", food);
startActivity(in);
}
to receive the values I created new ArrayList
in CartActivity
to get the data of the food that the amount in not zero.
// add components to array lists
// addList, removeList are methods to add,remove in my new ArrayList created in this class
for (int i = 0; i < amountList.length; i++) {
if (amountList[i] != 0) {
if (!foodListFinal.contains(foodList[i]))
addList(amountList[i], foodList[i], imgID[i]);
} else if (amountList[i] == 0 && !foodListFinal.isEmpty()) {
if (foodListFinal.contains(foodList[i])) {
index = foodListFinal.indexOf(foodList[i]);
removeList(amountList[index], foodList[index], imgID[index]);
}
}
}
Also, I have a method called addAmount
, minusAmount
in CartAcitivity
that will increase, decrease the amount just like in the MenuActivity
. It is called by an Adapter of this class which is not the same one with MenuActivity
.
public static void addAmount(int position) {
int newAmount = (amountListFinal.get(position)) + 1;
amountListFinal.set(position, newAmount);
}
public static void minusAmount(int position) {
int newAmount = (amountListFinal.get(position)) - 1;
amountListFinal.set(position, newAmount);
}
Here is my problem..
When I decrease amount
in the CartActivity
to 0 and I went back to MenuActivity
it correctly says 0, but if I switch to CartActivity
again the app crashes with an error saying index out of bound.
Please help me with this, or is there any better way to do this please enlighten me. Thank you