How to store HashMap on Android?

为君一笑 提交于 2019-11-27 06:15:21

问题


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 explain how to do that properly, please?

There are counters with their own names and values. I want to load them onсe when some activity was started, work with them (change, delete, add new), and then save that data to use next time. Right now I use HashMap because it's easy to delete/add values.

HashMap<String, Integer> counters;

回答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:

public abstract 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.




回答2:


Try this

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

and Another way is HERE




回答3:


There's a good example here.

So if your Map is like so:

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

The functions look like this:

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

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

The Generic code looks like this:

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


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

Replace YourObject with, well, your Java object.



来源:https://stackoverflow.com/questions/9227016/how-to-store-hashmap-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!