I have created a HashMap in the following way:
HashMap<Integer, String> buttons = new HashMap<Integer, String>();
I need this to remain in this format, however every answer to this solution I have seen only works for HashMap<String, String>
Thanks
I have created a HashMap in the following way:
HashMap<Integer, String> buttons = new HashMap<Integer, String>();
I need this to remain in this format, however every answer to this solution I have seen only works for HashMap<String, String>
Thanks
There is no support for any
HashMap
inSharedPreferences
. You can treat a wholeSharedPreferences
as being a bit like aHashMap
, but it is not aHashMap
, and you cannot store aHashMap
in an individual preference.You are welcome to convert your
HashMap
into aString
that could be stored in aSharedPreferences
value, such as by converting it into JSON.Hey I found a way in the end :)
I just changed the HashMap I had to format and then did the following to save the contents:
and the following to retrieve the HashpMap:
Converting it to JSON (as CommonsWare pointed out) is an option. Alternatively, the way I did it (since I'm not familiar with JSON) is to serialize it with Apache's ObjectSerializer class (found here: ObjectSerializer.java).
Simply call the serialize method when you want to store it and deserialize when you want to turn it back into a Map.