有没有办法ArrayList <ArrayList<Integer>> floors
通过Bundle将一个传递给另一个活动?
谢谢
有没有办法通过 Bundle 将 ArrayList > floor 传递给另一个活动?
不幸的是没有。
如果您ArrayList
没有嵌套,它将与putIntegerArrayList(key, value)
and一起使用getIntegerArrayList(key)
。
但是肯定有另一种方法。我将向您解释一种可能的方法。
您可以创建将实现 Serializable 接口的类,并在此类中创建字段和适当的 getter。我会给你一个基本的例子。然后你将通过活动传递可序列化。
public class DataHelper implements Serializable {
private ArrayList<ArrayList<Integer>> floors;
public DataHelper(ArrayList<ArrayList<Integer>> floors) {
this.floors = floors;
}
public ArrayList<ArrayList<Integer>> getList() {
return this.floors;
}
}
将其保存到捆绑包:
Bundle b = new Bundle();
b.putSerializable("floors", new DataHelper(floors));
并在目标活动中检索:
getIntent().getExtras().getSerializable("floors");
将数组列表从第一个活动传递到第二个活动。
Intent intent = new Intent(context, SecondActity.class);
intent.putIntegerArrayListExtra("arraylist",integerList); //integerList is ArrayList<Integer>
startActivity(intent);
在第二个 Activity 中获取 arrayList。
ArrayList arrayList<Integer> = getIntent().getIntegerArrayListExtra("arraylist")
在这里阅读。
如果您想在活动之间传递自定义对象,请阅读此线程。