0

我正在尝试通过意图传递自定义对象的哈希图。

我正在尝试使用 parcelable,因为我读到的是在 android 中做事的正确方法(即 parcelable over serializable)。

但是,当我尝试使用 getParcelable 获取哈希图时,它无法在意图中找到我的哈希图,但 getSerializable 可以。

可以对 hashmap 使用可序列化吗?这是我唯一的选择吗?

4

3 回答 3

3

你当然可以序列化你的地图,只要你的对象是可序列化的。您还可以做的是创建一个单独的单例类(DataStore 左右?)来保存您的数据。这样您就不需要将数据从 Activity 传递到 Activity。或者,更简单,将数据保存在公共静态变量中。

于 2013-11-02T00:31:42.387 回答
0

java.util.HashMap is not Parcelable, so your options are:

  • use put/getSerializable, which will work fine within your app
  • encode the HashMap as json or another remotable format if sending outside your app
  • take out the map entries one by one and put them in the Bundle directly (assuming they have string keys)
于 2013-11-02T03:35:02.413 回答
0

您必须添加一个“包装器”。这很丑陋,但工作......

/**
 * Provides a way to wrap a serializable {@link Map} in order to preserve its class
 * during serialization inside an {@link Intent}, otherwise it would be "flattened"
 * in a {@link android.os.Parcel} and unparceled as a {@link java.util.HashMap}.
 */
public class MapWrapper<T extends Map & Serializable> implements Serializable {

    private final T map;

    public MapWrapper(@NonNull T map) {
        this.map = map;
    }

    public T getMap() {
        return map;
    }

    /**
     * Add extra map data to the intent. The name must include a package prefix, for example
     * the app com.android.contacts would use names like "com.android.contacts.ShowAll".
     * <p>
     * The provided map will be wrapped to preserve its class during serialization.
     * Use {@link #getMapExtra(Intent, String)} to deserialize it.
     *
     * @param intent The intent to add data to.
     * @param name   The name of the extra data, with package prefix.
     * @param map    The map data value.
     *
     * @return The same {@link Intent} object, for chaining multiple calls into a single statement.
     *
     * @see Intent#putExtra(String, Serializable)
     */
    @NonNull
    public static <T extends Map & Serializable> Intent putMapExtra(
            @NonNull Intent intent, @NonNull String name, @NonNull T map) {
        return intent.putExtra(name, new MapWrapper<>(map));
    }

    /**
     * Retrieve extra map data from the intent.
     *
     * @param intent The intent to retrieve data from.
     * @param name   The name of the desired extra item.
     *
     * @return The value of an extra map item that was previously added with
     * {@link #putMapExtra(Intent, String, Map)} or {@code null} if no data was found.
     *
     * @throws ClassCastException
     * If the {@link Serializable} object with the specified name is not of type
     * {@link MapWrapper} or if the wrapped {@code Map} is not of type {@link T}.
     * 
     * @see Intent#getSerializableExtra(String)
     */
    @Nullable
    public static <T extends Map & Serializable> T getMapExtra(
            @NonNull Intent intent, @NonNull String name)
            throws ClassCastException {
        final Serializable s = intent.getSerializableExtra(name);
        //noinspection unchecked
        return s == null ? null : ((MapWrapper<T>)s).getMap();
    }

}

有关https://medium.com/the-wtf-files/the-mysterious-case-of-the-bundle-and-the-map-7b15279a794e的更多信息

于 2020-05-03T08:42:13.380 回答