62

I need to save on shared preferences some array of Strings and after that to get them. I tried this :

prefsEditor.putString(PLAYLISTS, playlists.toString()); where playlists is a String[]

and to get :

playlist= myPrefs.getString(PLAYLISTS, "playlists"); where playlist is a String but it is not working.

How can I do this?

starball
  • 20,030
  • 7
  • 43
  • 238
Gabrielle
  • 4,933
  • 13
  • 62
  • 122

5 Answers5

105

You can create your own String representation of the array like this:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < playlists.length; i++) {
    sb.append(playlists[i]).append(",");
}
prefsEditor.putString(PLAYLISTS, sb.toString());

Then when you get the String from SharedPreferences simply parse it like this:

String[] playlists = playlist.split(",");

This should do the job.

Egor
  • 39,695
  • 10
  • 113
  • 130
37

From API level 11 you can use the putStringSet and getStringSet to store/retrieve string sets:

SharedPreferences pref = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet(SOME_KEY, someStringSet);
editor.commit();

SharedPreferences pref = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
Set<String> someStringSet = pref.getStringSet(SOME_KEY);
MikeL
  • 5,385
  • 42
  • 41
  • i thinks it is easier than accepted answer if anyone building app from API level 11 or higher. – Sayem Jul 29 '15 at 08:57
  • 16
    The problem is that Set is not ordered. In contrast to String[] – Tomáš Hubálek Jul 30 '15 at 14:57
  • I have a string array with values as a,0 b,1 now on the click in settings, I want to changes these values. How to use putStringSet in this. – pa1pal Mar 30 '16 at 17:47
  • @pa1pal, you should probably create a new question and explain there exactly what you want to do and what you did up until now – MikeL Nov 27 '16 at 09:09
9

You can use JSON to serialize your array as a string and store it in the preferences. See my answer and sample code for a similar question here:

How can write code to make sharedpreferences for array in android?

Community
  • 1
  • 1
Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47
4
HashSet<String> mSet = new HashSet<>();
                mSet.add("data1");
                mSet.add("data2");
saveStringSet(context, mSet);

where

public static void saveStringSet(Context context, HashSet<String> mSet) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sp.edit();
    editor.putStringSet(PREF_STRING_SET_KEY, mSet);
    editor.apply();
}

and

public static Set<String> getSavedStringSets(Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    return sp.getStringSet(PREF_STRING_SET_KEY, null);
}

private static final String PREF_STRING_SET_KEY = "string_set_key";
Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
  • 4
    As noted by @Tomáš Hubálek, `Set` is not ordered and so could change the behavior of your program. – Dale Jun 19 '18 at 16:05
0

Store array list in prefrence using this easy function, if you want more info Click here

 public static void storeSerializeArraylist(SharedPreferences sharedPreferences, String key, ArrayList tempAppArraylist){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    try {
        editor.putString(key, ObjectSerializer.serialize(tempAppArraylist));
        editor.apply();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And how to get stored array list from prefrence

public static ArrayList getSerializeArraylist(SharedPreferences sharedPreferences, String key){
    ArrayList tempArrayList = new ArrayList();
    try {
        tempArrayList = (ArrayList) ObjectSerializer.deserialize(sharedPreferences.getString(key, ObjectSerializer.serialize(new ArrayList())));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tempArrayList;
}