我需要将Set放在SharedPreference中,但我遇到了问题。
当我点击按钮时,我将从SharedPreference获取Set并将数据添加到Set然后放回SharedPreference,但是当我销毁项目并再次打开它时,sharedPreference只在Set
中获得一个字符串SharedPreferences s = getSharedPreferences("db", 0);
Log.i("chauster", "1.set = "+s.getStringSet("set", new HashSet<String>()));
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences ss = getSharedPreferences("db", 0);
Set<String> hs = ss.getStringSet("set", new HashSet<String>());
hs.add(String.valueOf(hs.size()+1));
Editor edit = ss.edit();
edit.putStringSet("set", hs);
edit.commit();
SharedPreferences sss = getSharedPreferences("db", 0);
Log.i("chauster", "2.set = "+sss.getStringSet("set",
new HashSet<String>()));
}
});
当我首先安装项目时,我单击按钮4次,logcat打印它
1.set = []
2.set = [1]
2.set = [2, 1]
2.set = [3, 2, 1]
2.set = [3, 2, 1, 4]
将字符串放入sharedPreference Set看起来很成功,但是当我销毁应用并再次打开它时,logcat会将其打印出来
1.set = [1]
它表示在sharedPreference中只有一个字符串,我不知道发生了什么? 请帮我。感谢〜
答案 0 :(得分:39)
你陷入了编辑getStringSet()所得值的常见陷阱。这是禁止的in the docs
你应该:
SharedPreferences ss = getSharedPreferences("db", 0);
Set<String> hs = ss.getStringSet("set", new HashSet<String>());
Set<String> in = new HashSet<String>(hs);
in.add(String.valueOf(hs.size()+1));
ss.edit().putStringSet("set", in).commit(); // brevity
// SharedPreferences sss = getSharedPreferences("db", 0); // not needed
Log.i("chauster", "2.set = "+ ss.getStringSet("set", new HashSet<String>()));
对于半熟的解释,请参阅:Misbehavior when trying to store a string set using SharedPreferences
答案 1 :(得分:8)
在putStringSet
之前使用edit.clear()SharedPreferences ss = getSharedPreferences("db", 0);
Set<String> hs = ss.getStringSet("set", new HashSet<String>());
hs.add(String.valueOf(hs.size()+1));
Editor edit = ss.edit();
edit.clear();
edit.putStringSet("set", hs);
edit.commit();
答案 2 :(得分:2)
Remove
HashSet
中SharedPreferences
的密钥,commit
然后添加新值。
SharedPreferences ss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
ss = getSharedPreferences("db", 0);
fun();
}
void fun() {
Log.i("chauster", "1.set = "+ss.getStringSet("set", new HashSet<String>()));
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Set<String> hs = ss.getStringSet("set", new HashSet<String>());
hs.add(String.valueOf(hs.size()+1));
Log.i(TAG, "list: " + hs.toString());
Editor edit = ss.edit();
edit.remove("set");
edit.commit();
edit.putStringSet("set", hs);
Log.i(TAG, "saved: " + edit.commit());
Log.i("chauster", "2.set = "+ss.getStringSet("set", new HashSet<String>()));
}
});
}
答案 3 :(得分:-1)
尝试将SharedPreferences存储到静态变量,而不是每次都调用getSharedPreferences。这听起来很糟糕,但这对我有用了。
public class Prefs {
// this singleton is a workaround for an Android bug:
// two SharedPreferences objects do not see changes in each other.
private static SharedPreferences theSingletone;
public static SharedPreferences get(Activity from) {
//PreferenceManager.getDefaultSharedPreferences(getContext());
if (theSingletone == null) {
theSingletone = from.getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
}
return theSingletone;
}
}