我正在尝试更新我制作的密码生成器,以包含保存密码的功能,因此我使用的是SharedPreferences。我想将保存的密码放在Set中,但是当我尝试使用SharedPreferences.Editor的putStringSet方法保存集合时,Eclipse无法识别它。当我将鼠标悬停在我的代码中的putStringSet时,可用的2个快速修复程序是“更改为putString(...)”和“将强制转换添加到编辑器”,但我认为其中任何一个都没有帮助。
这就是我所拥有的:
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();
}
答案 0 :(得分:7)
putStringSet(...)
。我的猜测是你的目标是低于该版本。
答案 1 :(得分:2)
我使用putStringSet实现数据存储,然后需要将其反向移植到Gingerbread,因此我创建了一个名为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();
}
}
}
用法:
//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);
答案 2 :(得分:1)
要解决此问题,我创建了一个SharedPreferencesCompat类:
在String中以字符串 CSV 样式存储StringSet。
可以改变&#39;,&#39;由另一个分隔符在CVS中使用。
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();
}
}
注意:
它取决于apache StringUtils.join()方法。
您可以轻松地将其换成另一个。
答案 3 :(得分:0)
我知道这个线程是旧的,但我遇到了同样的问题,我的解决方案是使用全局HashTable来跟踪我的字符串,这实际上是文件名。我还需要与每个文件关联的状态,因此我实现了以下简单类:
public class Globals
{
public static enum SendStatus
{
NOT_SENT,
SEND_SUCCESS,
SEND_ERROR
}
public static volatile Hashtable<String, SendStatus> StoredFilesStatus;
}
}