Skip to content

Repository files navigation

HashMapXGson

implementation 'com.google.code.gson:gson:2.8.5'
//create test hashmapHashMap<String, String> testHashMap = newHashMap<String, String>();
testHashMap.put("key1", "value1");
testHashMap.put("key2", "value2");
//convert to string using gsonGsongson = newGson();
StringhashMapString = gson.toJson(testHashMap);
//save in shared prefsSharedPreferencesprefs = getSharedPreferences("test", MODE_PRIVATE);
prefs.edit().putString("hashString", hashMapString).apply();
//get from shared prefsStringstoredHashMapString = prefs.getString("hashString", "oopsDintWork");
java.lang.reflect.Typetype = newTypeToken<HashMap<String, String>>() {
}.getType();
HashMap<String, String> testHashMap2 = gson.fromJson(storedHashMapString, type);
//use valuesStringtoastString = testHashMap2.get("key1") + " | " + testHashMap2.get("key2");
Toast.makeText(this, toastString, Toast.LENGTH_LONG).show();

Copyright 2021 M. Fadli Zein

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages