Store and retrieve array list in Bundle in Android

2019-04-21 06:23发布

How do I store and retrieve an array list of values in a Bundle in Android? Any examples?

3条回答
一夜七次
2楼-- · 2019-04-21 06:43

Lets take if you array list has strings in it.

ArrayList<String> al = new ArrayList<String>();
al.add("test1");
al.add("test2");
al.add("test3");

Now you can put it into bundle as follows:

Bundle value= new Bundle();
value.putStringArrayList("temp1", al);

If you have your own object type instead of "String" in ArrayList then you need to serialize that ArrayList object.

查看更多
聊天终结者
3楼-- · 2019-04-21 06:57

Serialize your arraylist using the Parcelable class. You can add Parcelables to the Bundle.

查看更多
叼着烟拽天下
4楼-- · 2019-04-21 06:58

The only way to store an arraylist in bundle, if this arraylist can not be explain as an array of any primary type (int, char, etc...), is to serialize the data and to store it in the bundle as serializable.

查看更多
登录 后发表回答