I want to create Expandable List view for filter and i am storing check box status of every group into HashMap .but after apply filter first time and again going for the filter i am not able to retain state of check box.I want to store it into shared preference . can any one suggest how to do it.
Asked
Active
Viewed 577 times
1
-
http://stackoverflow.com/questions/14844605/how-can-i-store-a-hashmap-integer-string-in-android-using-shared-preferences – Vivek Mishra Apr 17 '17 at 11:16
-
I have all ready follow this post but not able to find out exact solution. – Amritesh Singh Apr 17 '17 at 11:23
-
http://stackoverflow.com/questions/35883831/android-saving-hashmap-to-sharedpreferences – Vivek Mishra Apr 17 '17 at 11:30
1 Answers
1
Inserting Into Shared Pref,
HashMap<Integer, boolean[]> hashmapB = new Hashmap<>;
After Adding Values to Hashmap then convert it 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);
Retrieve From Pref,
String hashmapB_String = Sellerregistration_Pref.getString("hashmapB_key",
(new JSONObject()).toString());
String To Hashmap (Here You Might Get Problem I just did this based on assumption),
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();
}

Mukeshkumar S
- 785
- 1
- 14
- 30
-
Above code is working fine but only if HashMap
hashmapB = new Hashmap<>; – Amritesh Singh Apr 17 '17 at 12:31 -