How to use Parceler with ArrayList<Map<String, String>>?

646 views Asked by At

I'm trying to use Parceler to transfer variable of ArrayList>, but it always give error when running, just link intent.putParcelableArrayListExtra("votedetail", arrlist);. Searched and found no example, how to use it? Thank you in advance.

3

There are 3 answers

0
B0Andrew On

Please take a look at the method's signature on Android Developer docs:

Intent putParcelableArrayListExtra (String name, ArrayList<? extends Parcelable> value)

The objects in the ArrayList have to be Parcelables. As far as I know, Android SDK implementations of Map<K,V> (HashMap, SortedMap, etc) do not implement Parcelable. You will have to write your own implementation which will also be Parcelable.

0
bluehale On

B0Andrew, Thank you for your help. I found the answer at last. Using putParcelableArrayList to do it. First wrap the

"Arraylist<Map<String, String>>" 

to become Arraylist(new one Arraylist, then add the variable), then I can use putParcelableArrayList to transfer the data.

0
John Ericksen On

Parceler is a library for serializing Parcelable objects. There is no need to use putParcelableArrayListExtra(), just use putParcelable():

List<Map<String, String>> value = new ArrayList<>();
// Fill out List

intent.putParcelable("votedetail", Parcels.wrap(value));

And you may deserialize in the onCreate of the receiving activity:

List<Map<String, String>> value = intent.getParcelableExtra("votedetail");

You can find examples on how to use it directly on the website: http://parceler.org/