0

Lets say, I have a

HashMap<String, String>

dictionary and I want to store in in my android device. I found out that I can not do that using shared preferences because I can only store a string set there and parsing it every time for storage is too much work. So how do I write and read a dictionary into/from phone's storage?

2 Answers2

0

Is the Hash too big? If not, then you can save all the key-value pairs in SharedPreferences.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("YOUR_KEY", "your_value");
editor.commit();
Jorge E. Hernández
  • 2,800
  • 1
  • 26
  • 50
  • Yeah, as I've said, I've seen this exact SharedPreferences solution on the official website and it didnt work for me –  Jul 29 '16 at 21:46
  • 1
    Got it! Have you tried SQLite? Not sure if realm.io fit your needs but you could give it a try. – Jorge E. Hernández Jul 29 '16 at 21:50
  • What i want is literally an example how i can write and read from a file in Android –  Jul 30 '16 at 13:37
0

You could indeed use SharedPreferences for that purpose and it is what I'd recommend in your case to store that simple data structure. It is not too much work as you mention as you can see in the following answer:

https://stackoverflow.com/a/9227098/812598

You could even optimize a little bit the answer of the link to avoid more I/O than you need by checking which of those keys have changed not to store all of them every time you call the save method.

Community
  • 1
  • 1
GoRoS
  • 5,183
  • 2
  • 43
  • 66