1

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

Rüdiger Hanke
  • 6,215
  • 2
  • 38
  • 45
emeraldhieu
  • 9,380
  • 19
  • 81
  • 139

3 Answers3

2

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

Felipe
  • 16,649
  • 11
  • 68
  • 92
1

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.

Alex Orlov
  • 18,077
  • 7
  • 55
  • 44
1

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

Community
  • 1
  • 1
Rookie
  • 8,660
  • 17
  • 58
  • 91