mohan
mohan

Reputation: 13165

Store and retrieve array list in Bundle in Android

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

Upvotes: 11

Views: 27097

Answers (3)

eMich
eMich

Reputation: 1736

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

Upvotes: 3

Umakant Patil
Umakant Patil

Reputation: 2308

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.

Upvotes: 21

Khetzal
Khetzal

Reputation: 286

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.

Upvotes: 1

Related Questions