开发者

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();
}


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


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);


To work around this issue I created a SharedPreferencesCompat class:

In stores the StringSet in a string CSV-style.

It is possible to change ',' used in CVS by another delimiter.

public class SharedPreferencesCompat {

  private final static String KEY_DELIMITER = "com.example.delimiter";

  public static void setStringSetDelimiter(final SharedPreferences sharedPreferences, final String delimiter) {
      final Editor editor = sharedPreferences.edit();
      editor.putString(KEY_DELIMITER, delimiter);
      editor.commit();
  }

  public static Set<String> getStringSet(final SharedPreferences sharedPreferences, final String key) {
      final Set<String> out = new LinkedHashSet<String>();
      final String base = sharedPreferences.getString(key, null);
      if (base != null) {
          out.addAll(Arrays.asList(base.split(sharedPreferences.getString(KEY_DELIMITER, ","))));
      }
      return out;
  }

  public static void putStringSet(final SharedPreferences sharedPreferences, final String key,
        final Set<String> stringSet) {
      final String concat = StringUtils.join(stringSet, sharedPreferences.getString(KEY_DELIMITER, ","));
      final Editor editor = sharedPreferences.edit();
      editor.putString(key, concat);
      editor.commit();
  }
}

Note:

It depends on apache StringUtils.join() method.

You can easily swap this out for another one.


I know this thread is a old, but I just ran into the same issue and my solution was to use a global HashTable to keep track of my strings, which were actually file names. I also needed a status associated with with each file, so I implemented the following simple class:

    public class Globals
    {
        public static enum SendStatus
        {
            NOT_SENT,
            SEND_SUCCESS,
            SEND_ERROR
        }

        public static volatile Hashtable<String, SendStatus> StoredFilesStatus;

        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜