【问题标题】:How to store and retrive HashMap<Integer, boolean[]> hashMapB=new HashMap<Integer,boolean[]>() into shared preferences in android如何在 android 中将 HashMap<Integer, boolean[]> hashMap=new HashMap<Integer,boolean[]>() 存储和检索到共享首选项中
【发布时间】:2017-04-17 11:14:33
【问题描述】:

我想为过滤器创建可扩展列表视图,并且我将每个组的复选框状态存储到 HashMap 中。但是在第一次应用过滤器后,我无法保留复选框的状态。我想要将其存储到共享首选项中。任何人都可以建议如何做到这一点。

【问题讨论】:

标签: java android


【解决方案1】:

插入共享首选项,

 HashMap<Integer, boolean[]> hashmapB = new Hashmap<>;

将值添加到 Hashmap 之后,然后将其转换为 JSON

JSONObject jsonObject = new JSONObject(hashmapB);
String jsonString = jsonObject.toString();

SharedPreferences keyValues = getSharedPreferences("Your_Shared_Prefs"), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = keyValues.edit();
editor.putString("hashmapB_key",jsonString);

从首选项中检索,

String hashmapB_String = Sellerregistration_Pref.getString("hashmapB_key",
                (new JSONObject()).toString());

String To Hashmap(在这里你可能会遇到问题我只是根据假设做了这个),

HashMap<Integer,boolean[]>hashmapB_Ret = new HashMap<>();

try {
    JSONObject jsonObject = new JSONObject(hashmapB_String);
    Iterator<String> keysItr = jsonObject.keys();
    while(keysItr.hasNext()) {
          String key = keysItr.next();
          boolean[] values = (boolean[]) jsonObject.get(key);
          hashmapB_Ret.put(Integer.valueOf(key), value);
    }
 } catch (JSONException e) {
        e.printStackTrace();
 }

【讨论】:

  • 以上代码工作正常,但前提是 HashMap hashmapB = new Hashmap;
  • @AmriteshSingh 哥们你还需要做什么
猜你喜欢
  • 2023-03-12
  • 2013-01-28
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
  • 2015-02-19
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多