I want to pass a List<Map<String, String>>
to another Activity. How to implement Parcelable for it?

- 6,215
- 2
- 38
- 45

- 9,380
- 19
- 81
- 139
-
1http://stackoverflow.com/questions/4141944/android-pass-listgeopoint-to-another-activity – Kazekage Gaara Jun 04 '12 at 06:44
-
Wouldn't you just add your List to the intent (with putExtra()) when you call your activity – gideon Jun 04 '12 at 06:45
-
@Gaara: It doesn't fit my needs. – emeraldhieu Jun 04 '12 at 06:46
-
http://stackoverflow.com/questions/7400564/android-parcelable-retailerorderactivity-java-return-null/7400675#7400675 – Lalit Poptani Jun 04 '12 at 06:47
-
the simplest way this make it "public static" – Tai Tran Jun 04 '12 at 06:55
-
Can anybody give me some code? It's too hard to implement. It is absolutely not like to the GeoPoint sample above. – emeraldhieu Jun 04 '12 at 07:04
-
Can't you use `putExtra (String name, Serializable value)` ? – Gubbel Jun 04 '12 at 07:08
-
@Gubbel: Is List – emeraldhieu Jun 04 '12 at 07:09
-
do you think you can store it in database and retuen a list? – Tai Tran Jun 04 '12 at 07:11
3 Answers
When possible, try to use Bundle
instead Map
. In your case, List<Map<String, String>>
a Bundle
can replace very well.
More info: http://developer.android.com/reference/android/os/Bundle.html

- 16,649
- 11
- 68
- 92
You could extend Map
and implement Parcelable
for it. When writing to parcel, you could write elements count as a first int
, then iterate over entries and add them one after another like:
destParcelable.writeInt(size());
for (final Entry<String, String> entry : getEntries()) {
destParcelable.writeString(entry.getKey());
destParcelable.writeString(entry.getValue());
}
When reading from parcel just read the first int
(that will be your entries count) and start a loop reading key and values one by one:
final int size = srcParcelable.readInt();
for (int i=0; i<size; i++) {
put(srcParcelable.readString(), srcParcelable.readString());
}
And I believe there is a method to add list of parcelables to bundle, and since your map is a parcelable now it won't be any problems.

- 18,077
- 7
- 55
- 44
You can do it like this: 1. Make a constructor of activity where you want to pass this list.
public Class CalledActivity extends Activity{
List<Map<String, String>> list;
public calledActivity(List<Map<String, String>> list){
this.list=list;
}
}
2. Call this constructor from the activity, you want to pass the list from.
See this question for reference. android activity class constructor working