20

My app has a list of String values (of some 5-20 characters each), that I have to store persistently. The SharedPreferences seem to me the most appropriate, as it's a short list. Usually it will be empty, or contain just a few values.

I've already seen this question which asks basically the same, the answer is unsuitable as the StringSet is API level 11, and I'm targeting Android 2.1 and up, which is API 7.

Considering the usually small list of strings, using a database seems total overkill to me. Using a file to store this would be another solution but not so elegant. I've considered creating keys (like "1", "2", "3", etc) but that's actually worse than using a file - especially when it comes to reading back my data.

Anyway if nothing else works I'll have to go for the file option. If my idea is simply not practical using the SharedPreferences for API lvl 7, I'd appreciate to hear that too.

Community
  • 1
  • 1
Wouter
  • 2,623
  • 4
  • 34
  • 43

3 Answers3

31

I found the easiest solution to store and retrieve a list of items from SharedPreferences is to simply serialize / deserilaize the array into / from JSON and store it into a string setting.

Gson comes really handy doing it.

READ:

SharedPreferences prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
String value = prefs.getString("list", null);

GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
MyObject[] list = gson.fromJson(value, MyObject[].class);

WRITE:

String value = gson.toJson(list);
SharedPreferences prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
Editor e = prefs.edit();
e.putString("list", value);
e.commit();
Drejc
  • 14,196
  • 16
  • 71
  • 106
  • 1
    You can use JSONObject/JSONArray too, which is included in Android by default, so no extra libs necessary. – PaulP Jul 10 '13 at 19:25
  • Need urgent help, how to get the list from the Shared preference ! Directly to a List @Drejc – Akash Dubey Feb 16 '17 at 06:53
  • How to Define MyObject Class! – Akash Dubey Feb 16 '17 at 06:56
  • Ok ... you are skipping ahead of yourself. First make sure that the class you try to write/read from the preferences can be serialized/deserialized into JSON - the class itself should be a simple value holding class (without any internal logic). Read up on JSON serialization with Gson. Once you have your Array MyObject[] you can transform it into a List with Arrays.asList() – Drejc Feb 17 '17 at 07:53
18

Using this object --> TinyDB--Android-Shared-Preferences-Turbo its very simple.

TinyDB tinydb = new TinyDB(context);

to put

tinydb.putList("MyUsers", mUsersArray);

to get

tinydb.getList("MyUsers");
kc ochibili
  • 3,103
  • 2
  • 25
  • 25
8

If you know the order of the list entries and/or it doesn't matter you can "glue" them all into a single String with a separator of your choice and then store it with SharedPreferences. After loading it back you would split the String into the entries based on your separator. Something like String listEntries = "enty1;entry2;entry3" (semicolon as separator in this case). But no idea about performance issues.

iDroid
  • 10,403
  • 1
  • 19
  • 27
  • Order doesn't matter. Basically same idea as when storing in a file but this is a little cleaner indeed. Will consider. – Wouter Jul 06 '11 at 15:07
  • 2
    Hack! There's gotta be a better way, right? Anyone? Bueller ... Bueller? Sigh, I'll probably have to do it that way (sooo OLD-school!). – SMBiggs Jun 18 '13 at 04:44