Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Q Beta - ContentValue is empty when created from parcel

Android Build: QPP5.190530.015

Emulator: Pixel 2

HashMap<String,Object> map = new HashMap<>();
map.put("key1", true);
map.put("key2", "String");
map.put("key3", 3);

Parcel parcel = Parcel.obtain();
parcel.writeMap(map);
parcel.setDataPosition(0);
ContentValues contentValues = ContentValues.CREATOR.createFromParcel(parcel);

expected result:

contentValues would contain the data of the given map.

actual result :

contentValues is empty

like image 516
Pravin Avatar asked Aug 02 '19 21:08

Pravin


1 Answers

I am also facing the same issue in createFromParcel with hashmap in Android Q ,solved it by by iteration through hashmap and casting the value to its respective type.

  private ContentValues getContentValuesFromHashMapValues(HashMap<String, Object> hashMap) {
            ContentValues contentValues = new ContentValues();
            for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
                Object value = entry.getValue();
                String key = entry.getKey();

                if (value instanceof Integer) {
                    contentValues.put(key, (Integer) value);
                } else if (value instanceof Long) {
                    contentValues.put(key, (Long) value);
                } else if (value instanceof Short) {
                    contentValues.put(key, (Short) value);
                } else if (value instanceof Float) {
                    contentValues.put(key, (Float) value);
                } else if (value instanceof Double) {
                    contentValues.put(key, (Double) value);
                } else if (value instanceof Byte) {
                    contentValues.put(key, (Byte) value);
                } else if (value instanceof Boolean) {
                    contentValues.put(key, (Boolean) value);
                } else if (value instanceof String) {
                    contentValues.put(key, ((value == null) ? "" : (String) value));
                }
            }

            return contentValues;
        }
like image 195
inder Avatar answered Nov 19 '22 18:11

inder