SharedPreferences putStringSet不起作用

时间:2014-01-28 03:37:22

标签: android sharedpreferences

我需要将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中只有一个字符串,我不知道发生了什么? 请帮我。感谢〜

4 个答案:

答案 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 HashSetSharedPreferences的密钥,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)

伙计们,我出于历史原因将它保存在这里。不要使用这种方法! (有什么用呢?它显示了代码出现的错误:API的行为与反直觉有关,有一个问题,开发人员试图解决问题。出现了错误的代码。后来,问题被正式记录,并且不同建议使用变通方法,但是错误的代码会被共享。)


尝试将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;
    }
}