Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在活动之间同步数组中的数据_Java_Android_Android Intent - Fatal编程技术网

Java 在活动之间同步数组中的数据

Java 在活动之间同步数组中的数据,java,android,android-intent,Java,Android,Android Intent,我正在制作一个食品订购应用程序。在我的主页上有一个名为MenuActivity的完整页面ListView,还有一个按钮,当我按下它时,它将指向CartActivity页面 我用数组food表示食物名称,imgID表示食物图像,butPlusbutMinus调整食物量,amount将每种食物的量保持为初始值0 我在MenuAcitivity中有一个名为addAmount,minusAmount的方法,它将增加或减少金额。它由此类的适配器调用 public static void addAmount

我正在制作一个食品订购应用程序。在我的主页上有一个名为
MenuActivity
的完整页面
ListView
,还有一个
按钮
,当我按下它时,它将指向
CartActivity
页面

我用数组
food
表示食物名称,
imgID
表示食物图像,
butPlus
butMinus
调整食物量,
amount
将每种食物的量保持为初始值0

我在
MenuAcitivity
中有一个名为
addAmount
minusAmount
的方法,它将增加或减少金额。它由此类的
适配器
调用

public static void addAmount(int position) {
    amount[position] += 1;
}
转到
CartActivity
,我用它来传递我的所有值

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);
}
为了接收值,我在
CartActivity
中创建了新的
ArrayList
,以获取数量不为零的食品数据

        // 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]);
            }
        }
    }
这是我的问题

当我将
CartActivity
中的
amount
减少到0,然后返回到
MenuActivity
时,它正确地显示为0,但如果我再次切换到
CartActivity
,应用程序会崩溃,并出现一个错误,称为index out bound


请帮我做这个,或者有没有更好的方法请启发我。感谢您

是哪个数组导致了此错误:“说索引超出范围。”、foodList还是amountList数组?它将帮助您

我建议使用辅助存储来存储您的更改。也不建议将整个数组传递给其他活动。请使用任何数据库在其中插入更改,然后在购物车活动中检索所有数据。
这样,您可以在各种对象中实现相同的数量状态。

谢谢!!我想我正在使用SQLite:DThanks来实现它!!我现在算出来了:)
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);
}