Android SharedPreferences putStringSet 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21396358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SharedPreferences putStringSet doesn't work
提问by henry4343
I need to put Set in SharedPreference, but I have a problem.
我需要将 Set 放在 SharedPreference 中,但是我遇到了问题。
when I click button, I will get Set from SharedPreference and add data to Set then put back SharedPreference, but when I destroy project and open it again, the sharedPreference only get one string in Set
当我单击按钮时,我将从 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>()));
}
});
when I install project first, and I click button 4 times, the logcat print it
当我先安装项目时,我点击按钮 4 次,logcat 打印它
1.set = []
2.set = [1]
2.set = [2, 1]
2.set = [3, 2, 1]
2.set = [3, 2, 1, 4]
it's look like success to put string in sharedPreference Set, but when I destroy app and open it again, the logcat print it
将字符串放入 sharedPreference Set 看起来很成功,但是当我销毁应用程序并再次打开它时,logcat 会打印它
1.set = [1]
it means only one string in Set from sharedPreference, I don't know what's happened? Please help me. thanks~
这意味着sharedPreference中只有一个字符串,我不知道发生了什么?请帮我。谢谢~
采纳答案by henry4343
use edit.clear() before putStringSet
在 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();
回答by Mr_and_Mrs_D
You fell to the usual trap of editing the value you got from getStringSet(). This is forbidden in the docs
您陷入了编辑从 getStringSet() 获得的值的常见陷阱。这在文档中是禁止的
You should :
你应该 :
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>()));
For a half baked explanation see : Misbehavior when trying to store a string set using SharedPreferences
有关半成品的解释,请参阅:尝试使用 SharedPreferences 存储字符串集时的行为不当
回答by Krrishnaaaa
Removethe key for HashSetin SharedPreferences, committhen add new value.
RemoveHashSetin的键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>()));
}
});
}
回答by 18446744073709551615
Folks, I'm keeping it here for historical reasons. Do not use this approach! (What use is it? It shows how bad code appears: the API behaves counter-intuitively, there is a gotcha, a developer tries to work-around the gotcha. Bad code appears. Later, the gotcha gets officially documented, and a different workaround is suggested, but the bad code gets shared.)
伙计们,我把它留在这里是出于历史原因。不要使用这种方法!(它有什么用?它显示了糟糕的代码出现的程度:API 的行为违反直觉,有一个问题,开发人员试图解决这个问题。出现了错误的代码。后来,这个问题得到了正式的文档,并且一个不同的建议解决方法,但错误的代码会被共享。)
Try to store SharedPreferences to a static variable instead of invoking getSharedPreferences each time. This sounds terrible, but this worked for me once.
尝试将 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;
}
}

