简体   繁体   English

Android:如何在sharedpreferences中存储和检索Hashmap数据?

[英]Android:how to store and retrieve Hashmap data in sharedpreferences?

I want to store some data like key and value pair in internal storage. 我想在内部存储中存储键和值对等一些数据。 so i am using HashMap to store key,value pair .but i don'get sufficient solution to store and retrieve this HashMap into sharedpreferences. 所以我使用HashMap来存储密钥,值对。但是我没有足够的解决方案来存储和检索这个HashMap到共享偏好中。 Please give me some solution. 请给我一些解决方案。

i am put my code below: 我把我的代码放在下面:

HashMap<String, String> MapListMesasges = new HashMap<String, String>();
MapListMesasges.put(FromName, message.getBody());
preferences = mActivity.getSharedPreferences(
                                SharePreference_messages_history_name,
                                Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        for (Entry<String, String> entry : MapListMesasges
                                .entrySet()) {
                            editor.putString(entry.getKey(), entry.getValue());
                        }
                        editor.commit();

and retrieve data from Sharedprefernces: 并从Sharedprefernces检索数据:

preferences = mActivity.getSharedPreferences(
                SharePreference_messages_history_name, Context.MODE_PRIVATE);
        for (Entry<String, ?> entry : preferences.getAll().entrySet()) {
            MapListMesasges.put(entry.getKey(), entry.getValue().toString());
        }

i will also store hashmap in adapter and set into listview. 我还将在适配器中存储hashmap并设置为listview。 My main purpose is to store and retrieve data into sharedprefernces and also show data into listview. 我的主要目的是将数据存储和检索到共享偏好中,并将数据显示到listview中。

Thanks in advance. 提前致谢。

The easiest way to store HashMap in Preferences is just convert it to the string and store it. 在Preferences中存储HashMap的最简单方法是将其转换为字符串并存储它。

On the Otherside get that string convert it in HashMap and enjoy. 在Otherside获取该字符串将其转换为HashMap并享受。

To Convert String to HashMap & vice-versa check this link. 要将字符串转换为HashMap,反之亦然,请检查链接。

You can use json for solution. 您可以使用json进行解决方案。

For example: 例如:

**JSON**

    {
     [
      "name1","message1",
      "name2","message2"
     ]
    }

Store this entire json string with some key and use same key to retrieve. 使用一些密钥存储整个json字符串并使用相同的密钥进行检索。 Just you need to parse/create JSON. 只需要解析/创建JSON。 You can use GSON lib for JSON creation/parsing 您可以使用GSON lib进行JSON创建/解析

There's special thing for doing operations like you described and this is SQLite . 像你所描述的那样进行操作是特别的,这就是SQLite But if you really want to store them in SharedPreferences, you can do it. 但是如果你真的想将它们存储在SharedPreferences中,你就可以做到。 Your solution almost work. 您的解决方案几乎可行 You just need to store keys as well. 您只需要存储密钥。

StringBuilder b = new StringBuilder();
for (Entry<String, String> entry : MapListMesasges.entrySet()) {
      editor.putString(entry.getKey(), entry.getValue());
      if(b.length() != 0){
           b.append(",");
      }
      b.append(entry.getKey());
 }
editor.putString("KEY_FOR_PREF_KEYS",b.toString());

And to retrieve data use this 并检索数据使用此

String[] keys = preferences.getString("KEY_FOR_PREF_KEYS","").split(",");
for (int i = 0; i < keys.length; i++) {
    MapListMesasges.put(keys[i], preferences.getString(keys[i]));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM