Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How write Java.util.Map into parcel in a smart way?

I have a Generic Map of Strings (Key, Value) and this field is part of a Bean which I need to be parcelable. So, I could use the Parcel#writeMap Method. The API Doc says:

Please use writeBundle(Bundle) instead. Flattens a Map into the parcel at the current dataPosition(), growing dataCapacity() if needed. The Map keys must be String objects. The Map values are written using writeValue(Object) and must follow the specification there. It is strongly recommended to use writeBundle(Bundle) instead of this method, since the Bundle class provides a type-safe API that allows you to avoid mysterious type errors at the point of marshalling.

So, I could iterate over each Entry in my Map a put it into the Bundle, but I'm still looking for a smarter way doing so. Is there any Method in the Android SDK I'm missing?

At the moment I do it like this:

final Bundle bundle = new Bundle(); final Iterator<Entry<String, String>> iter = links.entrySet().iterator(); while(iter.hasNext()) {     final Entry<String, String>  entry =iter.next();     bundle.putString(entry.getKey(), entry.getValue()); } parcel.writeBundle(bundle); 
like image 375
Kitesurfer Avatar asked Nov 24 '11 09:11

Kitesurfer


People also ask

Are maps Parcelable?

As we've said before, this is expected, as Map implementations are Serializable , and they aren't Parcelable . There also isn't a putExtras() that explicitly takes a Map .

What is Parcelable?

Parcelable is a serialization mechanism provided by Android to pass complex data from one activity to another activity.In order to write an object to a Parcel, that object should implement the interface “Parcelable“.

Can we create object of map in Java?

Java does not allow creating objects using an interface, and the same is true for maps too. Hence, you need classes that implement the map interface to create objects of this type. You can use any of the three classes that implement the map interface and use them for creating map objects.


2 Answers

I ended up doing it a little differently. It follows the pattern you would expect for dealing with Parcelables, so it should be familiar.

public void writeToParcel(Parcel out, int flags){   out.writeInt(map.size());   for(Map.Entry<String,String> entry : map.entrySet()){     out.writeString(entry.getKey());     out.writeString(entry.getValue());   } }  private MyParcelable(Parcel in){   //initialize your map before   int size = in.readInt();   for(int i = 0; i < size; i++){     String key = in.readString();     String value = in.readString();     map.put(key,value);   } } 

In my application, the order of the keys in the map mattered. I was using a LinkedHashMap to preserve the ordering and doing it this way guaranteed that the keys would appear in the same order after being extracted from the Parcel.

like image 139
Anthony Naddeo Avatar answered Oct 17 '22 23:10

Anthony Naddeo


you can try:

bundle.putSerializable(yourSerializableMap); 

if your chosen map implements serializable (like HashMap) and then you can use your writeBundle in ease

like image 42
STT LCU Avatar answered Oct 17 '22 22:10

STT LCU