How do I store and retrieve an array list of values in a Bundle
in Android? Any examples?
Asked
Active
Viewed 2.6k times
10
3 Answers
20
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.
Umakant Patil
- 2,035
- 4
- 25
- 54
-
1I'm wondering what is the reasoning behind these API methods using ArrayLists rather than Lists? – jramoyo Jul 24 '13 at 03:38
-
@jramoyo You can use list, but mohan asked for ArrayList so. – Umakant Patil Jul 25 '13 at 11:12
3
Serialize your arraylist using the Parcelable class. You can add Parcelables to the Bundle.
Vadim Kotov
- 7,103
- 8
- 44
- 57
eMich
- 1,736
- 1
- 14
- 16
1
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.
Khetzal
- 286
- 1
- 5