How can I store HashMap <Integer, String> in android using general settings?

I created a HashMap like this:

HashMap<Integer, String> buttons = new HashMap<Integer, String>();

I need it to stay in this format, however every answer to this solution I have seen only works for HashMap<String, String>

thank

+3


source to share


3 answers


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() );

      

+14


source


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.

+4


source


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.

0


source







All Articles