Writes a Map to a Parcel using a String array and a Bundle. - Android Android OS

Android examples for Android OS:Parcel

Description

Writes a Map to a Parcel using a String array and a Bundle.

Demo Code


//package com.java2s;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Map;
import java.util.Set;

public class Main {
    /**/* w  ww  .  j  av a  2 s.  c o m*/
     * Writes a Map to a Parcel using a String array and a Bundle.
     *
     * @param map the Map<String,V> to store in the parcel
     * @param in  the Parcel to store the map in
     */
    public static void writeMap(Map<String, ? extends Parcelable> map,
            Parcel out) {

        if (map != null && map.size() > 0) {
            Set<String> keySet = map.keySet();
            Bundle b = new Bundle();
            for (String key : keySet)
                b.putParcelable(key, map.get(key));
            String[] array = keySet.toArray(new String[keySet.size()]);
            out.writeStringArray(array);
            out.writeBundle(b);
        } else {
            //String[] array = Collections.<String>emptySet().toArray(new String[0]);
            // you can use a static instance of String[0] here instead
            out.writeStringArray(new String[0]);
            out.writeBundle(Bundle.EMPTY);
        }
    }
}

Related Tutorials