Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SharedPreferences.Editor putStringSet method not available

I am trying to update a password generator I made to include the ability to save passwords, so I'm using SharedPreferences. I want to put the saved passwords inside a Set, but when I try to save the set using SharedPreferences.Editor's putStringSet method, Eclipse does not recognize it. When I hover over putStringSet in my code, the 2 quick fixes available are "Change to putString(...)" and "add cast to editor", but I don't think either of those helps.

This is what I have:

public void save(View v)
{
    SharedPreferences prefs = getPreferences(MODE_PRIVATE);
    final SharedPreferences.Editor editor = prefs.edit();
    savedPasswords = (Set<String>) getSharedPreferences("savedPasswordsList", 0);
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setItems(passwords, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface face, int num)
        {
            savedPasswords.add(passwords[num]);
            editor.putStringSet("savedPasswordsList", savedPasswords);
            editor.commit();
            refreshSavedPasswordsList();
        }
    });
    dialog.show();
}
like image 471
Pat Needham Avatar asked Dec 22 '25 22:12

Pat Needham


2 Answers

putStringSet(...) was added at API 11 (Android v3.0.x onwards). My guess is you're targeting a version below that.

like image 176
Squonk Avatar answered Dec 24 '25 10:12

Squonk


I implemented data storage using putStringSet and then needed to backport it to Gingerbread so I created a small class called JSONSharedPreferences.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.SharedPreferences;


    public class JSONSharedPreferences {
      private static final String PREFIX = "json";

        public static void saveJSONObject(SharedPreferences prefs, String key, JSONObject object) {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(JSONSharedPreferences.PREFIX+key, object.toString());
            editor.commit();
        }

        public static void saveJSONArray(SharedPreferences prefs, String key, JSONArray array) {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(JSONSharedPreferences.PREFIX+key, array.toString());
            editor.commit();
        }

        public static JSONObject loadJSONObject(SharedPreferences prefs, String key) throws JSONException {
            return new JSONObject(prefs.getString(JSONSharedPreferences.PREFIX+key, "{}"));
        }

        public static JSONArray loadJSONArray(SharedPreferences prefs, String key) throws JSONException {
            return new JSONArray(prefs.getString(JSONSharedPreferences.PREFIX+key, "[]"));
        }

        public static void remove(SharedPreferences prefs, String key) {
            if (prefs.contains(JSONSharedPreferences.PREFIX+key)) {
                SharedPreferences.Editor editor = prefs.edit();
                editor.remove(JSONSharedPreferences.PREFIX+key);
                editor.commit();
            }
        }
    }

Usage:

//Below, the code to use putStringSet is commented out. 
//Alternative JSONSharedPreferences is used instead 

//Set<String> trainers = new TreeSet<String>();     
JSONArray jTrainers = new JSONArray();
List<FilteredWorkoutVideo> videos = getAllFilteredVideos(prefs);
for (FilteredWorkoutVideo video : videos) {
    //trainers.add(video.getTrainerName()); 
  jTrainers.put(video.getTrainerName());
}

//e = prefs.edit();
//e.putStringSet(Constants.KEY_ALL_TRAINERS, trainers);
//e.apply();
JSONSharedPreferences.saveJSONArray(prefs, Constants.KEY_ALL_TRAINERS, jTrainers);
like image 32
Damian Avatar answered Dec 24 '25 12:12

Damian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!