How can I store HashMap <Integer, String> in android using general settings?
Hey i found a way at the end :)
I just changed the HashMap which I had to format and then did the following to save the content:
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, 0).edit();
for( Entry entry : backUpCurency_values.entrySet() )
editor.putString( entry.getKey(), entry.getValue() );
editor.commit();
and to get the HashpMap:
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
for( Entry entry : prefs.getAll().entrySet() )
backUpCurency_values.put( entry.getKey(), entry.getValue().toString() );
source to share
There HashMap
is SharedPreferences
no support . You can treat the whole SharedPreferences
as a little like HashMap
, but it is not HashMap
, and you cannot keep HashMap
to individual preference.
You can convert HashMap
to String
which can be stored in a value SharedPreferences
, for example by converting it to JSON.
source to share
Converting it to JSON (as noted by CommonsWare) is an option. Also, the way I did it (since I'm not familiar with JSON) is to serialize it using the Apache ObjectSerializer class (here: ObjectSerializer.java ).
Just call the serialize method when you want to save it and deserialize when you want to put it back in the map.
source to share