Android HashMap in Bundle?

后端 未结 7 2391
眼角桃花
眼角桃花 2020-12-02 11:57

The android.os.Message uses a Bundle to send with it\'s sendMessage-method. Therefore, is it possible to put a HashMap inside a

7条回答
  •  青春惊慌失措
    2020-12-02 12:29

      public static Bundle mapToBundle(Map data) throws Exception {
        Bundle bundle = new Bundle();
        for (Map.Entry entry : data.entrySet()) {
            if (entry.getValue() instanceof String)
                bundle.putString(entry.getKey(), (String) entry.getValue());
            else if (entry.getValue() instanceof Double) {
                bundle.putDouble(entry.getKey(), ((Double) entry.getValue()));
            } else if (entry.getValue() instanceof Integer) {
                bundle.putInt(entry.getKey(), (Integer) entry.getValue());
            } else if (entry.getValue() instanceof Float) {
                bundle.putFloat(entry.getKey(), ((Float) entry.getValue()));
            }
        }
        return bundle;
    }
    

提交回复
热议问题