Write a HashMap to a Parcel, class of key is String, class of Value can parcelable - Android android.os

Android examples for android.os:Parcel

Description

Write a HashMap to a Parcel, class of key is String, class of Value can parcelable

Demo Code


//package com.java2s;

import java.util.Map;
import java.util.Map.Entry;
import android.os.Parcel;
import android.os.Parcelable;

public class Main {
    /**//from  w w w.ja v  a2 s . c  o m
     * Write a HashMap to a Parcel, class of key is String, class of Value can parcelable
     *
     * @param map
     * @param out
     * @param flags
     */
    public static <V extends Parcelable> void writeHashMapStringKey(
            Map<String, V> map, Parcel out, int flags) {
        if (map != null) {
            out.writeInt(map.size());

            for (Entry<String, V> entry : map.entrySet()) {
                out.writeString(entry.getKey());
                out.writeParcelable(entry.getValue(), flags);
            }
        } else {
            out.writeInt(-1);
        }
    }
}

Related Tutorials