Reputation: 3708
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.
Upvotes: 0
Views: 731
Reputation: 5158
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();
Upvotes: 0
Reputation: 2624
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
.
Upvotes: 0
Reputation: 466
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.
Upvotes: 5