Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store a List or Set in SharedPreferences

Tags:

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.

like image 289
Wouter Avatar asked Jul 06 '11 14:07

Wouter


People also ask

How do I save a List in shared preferences?

You can save String and custom array list using Gson library. =>First you need to create function to save array list to SharedPreferences. public void saveListInLocal(ArrayList<String> list, String key) { SharedPreferences prefs = getSharedPreferences("AppName", Context. MODE_PRIVATE); SharedPreferences.

Can I store List in SharedPreferences flutter?

Flutter's shared_preferences plugin has a method: setStringList(String key, List<String> value) , so you can just write serializer for your objects. Thanks.

What is difference between preferences and SharedPreferences?

Preferences: The user interfaces part of the settings. It contains different classes which allow one to compose Settings screens from code or XML. Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you.

Is SQLite better than SharedPreferences?

To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through.


1 Answers

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();
like image 190
Drejc Avatar answered Nov 23 '22 23:11

Drejc