Skip to content Skip to sidebar Skip to footer

Android Implement Parcelable Object Which Has Hashmap

I have classes like this. public class RateData { Map> rateMap; } public class RateDTO { private String code; private String nam

Solution 1:

You should make DateRTOParcelable - this should be trivial. Then,

Map<String, List<RateDTO>> map;

publicvoidwriteToParcel(Parcel out, int flags) {
    out.writeInt(map.size());

    for (Map.Entry<String, List<RateDTO>> entry : map.entrySet()) {
        out.writeString(entry.getKey());

        final List<RateDTO> list = entry.getValue();
        finalintlistLength= list.size();

        out.writeInt(listLength);

        for (RateDTO item: list) {
            out.writeParcelable(item, 0);
        }
    }
}

privateMyParcelable(Parcel in) {
    finalintsize= in.readInt();

    for (inti=0; i < size; i++) {
        finalStringkey= in.readString();
        finalintlistLength= in.readInt();

        final List<RateDTO> list = newArrayList<RateDTO>(listLength);
        for (intj=0; j < listLength; j++) {
            finalRateDTOvalue= in.readParcelable(ParentClass.class.getClassLoader());
            list.add(value);
        }

        map.put(key, list);
    }
}

I haven't tested the code, but I believe it should be close to OK. Note that this solution is not particularly good if your map is relatively large.

Post a Comment for "Android Implement Parcelable Object Which Has Hashmap"