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

Android examples for android.os:Parcel

Description

Read a HashMap from a Parcel, class of key is String, class of Value can parcelable

Demo Code


//package com.java2s;
import java.util.HashMap;
import java.util.Map;

import android.os.Parcel;
import android.os.Parcelable;

public class Main {
    /**//from  w w  w . j a va  2 s .c om
     * Read a HashMap from a Parcel, class of key is String, class of Value can parcelable
     *
     * @param <V>
     * @param in
     * @param loader
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <V extends Parcelable> Map<String, V> readHashMapStringKey(
            Parcel in, ClassLoader loader) {
        if (in == null) {
            return null;
        }

        int size = in.readInt();
        if (size == -1) {
            return null;
        }

        Map<String, V> map = new HashMap<String, V>();
        for (int i = 0; i < size; i++) {
            String key = in.readString();
            map.put(key, (V) in.readParcelable(loader));
        }
        return map;
    }
}

Related Tutorials