Skip to content Skip to sidebar Skip to footer

How To Store Hashmap On Android?

So, I'm trying to store HashMap on Android. I think it's better to use internal storage, but I don't understand how to save HashMap in it and then read it later. Can someone explai

Solution 1:

SharedPreferences also store data in key-value pair as hashmap, so why not get all key-values from hashmap and store into map, as it:

SharedPreferences pref= getContext().getSharedPreferences("Your_Shared_Prefs"), 
                                                           Context.MODE_PRIVATE);
SharedPreferences.Editor editor= pref.edit();

    for (String s : map.keySet()) {
        editor.putString(s, map.get(s));
    }

To fetch values you can use:

publicabstract Map<String, ?> getAll ()

http://developer.android.com/reference/android/content/SharedPreferences.html#getAll%28%29

use:

SharedPreferences pref= getContext().getSharedPreferences("Your_Shared_Prefs"), 
                                                           Context.MODE_PRIVATE);
HashMap<String, String> map= HashMap<String, String> pref.getAll();
for (String s : map.keySet()) {
        String value=map.get(s);
        //Use Value
    }

Code is not compiled, so it may have some minor errors, but should work.

Solution 2:

Try this

HashMap<String, String> hashMap = newHashMap<String, String>();
hashMap.put("key", "value");
Intent intent = newIntent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);

and Another way is HERE

Solution 3:

There's a good example here.

So if your Map is like so:

HashMap<String, byte[]> = new HashMap<>();

The functions look like this:

publicvoidsaveHashMap(String key , Object obj) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    Gson gson = newGson();
    String json = gson.toJson(obj);
    editor.putString(key,json);
    editor.apply();     // This line is IMPORTANT !!!
}

publicHashMap<String,byte[]> getHashMap(String key) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    Gson gson = newGson();
    String json = prefs.getString(key,"");
    java.lang.reflect.Typetype = newTypeToken<HashMap<String,byte[]>>(){}.getType();
    HashMap<String,byte[]> obj = gson.fromJson(json, type);
    return obj;
}

The Generic code looks like this:

publicvoidsaveHashMap(String key , Object obj) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    SharedPreferences.Editor editor = prefs.edit();
    Gson gson = newGson();
    String json = gson.toJson(obj);
    editor.putString(key,json);
    editor.apply();     // This line is IMPORTANT !!!
}


publicHashMap<Integer,YourObject> getHashMap(String key) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    Gson gson = newGson();
    String json = prefs.getString(key,"");
    java.lang.reflect.Typetype = newTypeToken<HashMap<Integer,YourObject>>(){}.getType();
    HashMap<Integer,YourObject> obj = gson.fromJson(json, type);
    return obj;
}

Replace YourObject with, well, your Java object.

Post a Comment for "How To Store Hashmap On Android?"