单击另一个 activity 添加到 ArrayList

Adding to an ArrayList on click from another activity

每次单击另一个 activity 的按钮时,我都试图继续添加到数组列表中。

arrayList = new ArrayList<>();
                    arrayList.add(name);
                    arrayList.add(price);
                    arrayList.add(quantity);

使用共享首选项并在完成后清除它们

首先,您应该向我们提供更多信息。 关于您现在的问题,我想您可以创建一个可序列化对象而不是列表,如下所示:

public class CustomObject implements Serializable{
private String name;
private double price;
private int quantity;
// constructor etc here
}

那么您可以像这样在活动之间传递对象:

Intent i = new Intent(CurrentActivity.this, NewActivity.class);    
i.putExtra("object", customObject);
startActivity(i);

然后在你的新 activity:

Intent i = getIntent();
CustomObject obj = i.getSerializableExtra("object");

将一个列表从一个 activity 传递到另一个的工作方式类似,但我发现您使用的列表不正确,没有声明它存储的对象类型。我猜名称将是一个字符串,数量将是一个数字...

无论如何,以下是跨活动传递列表的方法:

Intent i = new Intent(CurrentActivity.this, NewActivity.class);    
i.putExtra("list", arrayList);
startActivity(i);

然后在你的新 activity:

Intent i = getIntent();
arrayList = i.getSerializableExtra("list");