开发者

Store and retrieve array list in Bundle in Android

开发者 https://www.devze.com 2023-01-27 15:49 出处:网络
How do I store and retrieve an a开发者_如何学编程rray list of values in a Bundle in Android? Any examples?Lets take if you array list has strings in it.

How do I store and retrieve an a开发者_如何学编程rray list of values in a Bundle in Android? Any examples?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号