Store and retrieve array list in Bundle in Android

本小妞迷上赌 提交于 2019-12-22 03:47:03

问题


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


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/4303800/store-and-retrieve-array-list-in-bundle-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!