0

I'm trying to store the Checkbox text and it's state which is a boolean inside a Shared Preference. I'm having the issue - Incompatible types Entry<String, Capture<?>> found Entry<String, java.lang.Boolean> required when retrieving the value (at this line Map.Entry<String, Boolean> entry4)

This is how I'm storing the values inside shared preferences -

SharedPreferences checkedFilterPref = getContext().getSharedPreferences(
                        Constants.FILTER_CHECKED_PREF, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = checkedFilterPref.edit();
for (Map.Entry<String, Boolean> entry1 : filterPrefHashMap.entrySet()) {
    editor.putBoolean(entry1.getKey(), entry1.getValue());
}
editor.commit();

This is how I'm trying to retrieve the HashMap values -

for (Map.Entry<String, Boolean> entry4 : checkedFilterPref.getAll().entrySet()) {
    filterPrefHashMap.put(entry4.getKey(), entry4.getValue());
}

I'm following the technique shared in this StackOverflow post. I can't seem to get it right. Any help can be appreciated.

Community
  • 1
  • 1
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
  • What exactly doesn't work? DO you get exceptions? Do the values not get loaded? Does your app jsut stop? Does android studio crash? Details please. – Squirrelkiller Sep 12 '16 at 10:37
  • `checkedFilterPref.getAll().entrySet()` returns what type of data? I doubt it is `HashMap` considering you can store more than just Boolean in a SharedPreferences – OneCricketeer Sep 12 '16 at 10:37
  • Personally, I would go with the JSON conversion route – OneCricketeer Sep 12 '16 at 10:39
  • @cricket_007 `checkedFilterPref.getAll().entrySet()` returns `Entry>` that is the issue. I want it to return `HashMap` so that I can continue with my code. – Srikar Reddy Sep 12 '16 at 10:41
  • @MarlonRegenhardt It is a fatal error. Error:(269, 89) error: incompatible types: Entry cannot be converted to Entry where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? – Srikar Reddy Sep 12 '16 at 10:43
  • What is wrong with the answer that was given? You can simple use `Entry` with no types, then you could use `Boolean.valueOf` on the string value of the object. There is no way to get just the Boolean values of your SharedPreferences – OneCricketeer Sep 12 '16 at 10:44
  • 2
    Anyways, this is the answer that you want http://stackoverflow.com/a/26709199/2308683 – OneCricketeer Sep 12 '16 at 10:46
  • @cricket_007 the previous solution you've shared worked. – Srikar Reddy Sep 12 '16 at 11:08
  • Possible duplicate of [How to save HashMap to Shared Preferences?](https://stackoverflow.com/questions/7944601/how-to-save-hashmap-to-shared-preferences) – Iulian Popescu Mar 16 '18 at 21:55

3 Answers3

5

You can save primitive data types in Shared Preferences. So the easiest method to save HashMap into Shared Preferences is to convert the HashMap in Json string and then save that string in Shared Preferences.

And while retrieving it back you will have to convert string to HashMap again.

Ashish Rajvanshi
  • 466
  • 4
  • 15
0

You can try doing this:

File file = new File(getDir("data", MODE_PRIVATE), "map");
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(map);
outputStream.flush();
outputStream.close();

Source

Community
  • 1
  • 1
Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • Same comment as the post you've copied... How do you *read* from it? – OneCricketeer Sep 12 '16 at 11:12
  • Sorry, my bad. In this case, use Gson or any other kind of serialization to encode hash-map into a String then you can save it. As example, you can create a class wich contans that hash-map, constructor, getter, etc and then serializate it to a String doing this: `new Gson().toJson(object)` and to retrieve: `new Gson().fromJson(savedObjectAsStringFromSharedPreferences, ClassThatContainsHashMapObj.class);` – Cătălin Florescu Sep 12 '16 at 11:23
0

You can convert each Entry to string like this:

abc_true, def_false;

Then save them to SharePreference as strings.

When you read data from SharePreference, you have to convert the strings to Entry.

TOP
  • 2,574
  • 5
  • 35
  • 60