android SharedPreferences中的Set.add()没有按预期工作

时间:2015-08-22 15:37:30

标签: android

我试图从SharedPreferences首选项中获取一个字符串,然后将其放回另一个SharedPreferences首选项中。

来自第一个偏好的getString()" test1"并使用putStringSet()将其添加到第二个首选项。

示例代码:

void test()
{
    Log.v("Main", "********");

    final SharedPreferences test1 = context.getSharedPreferences("test1", MODE_PRIVATE);
    final SharedPreferences.Editor editor1 = test1.edit();
    editor1.clear();
    editor1.putString("sample1", "This is a test");
    editor1.apply();

    final SharedPreferences test2 = context.getSharedPreferences("test2", MODE_PRIVATE);
    final Set<String> set = new HashSet<>(test2.getStringSet("sample2", new HashSet<String>()));

    Log.v("Main", "set.size: " + set.size());

    Log.v("Main", test1.getString("sample1", null));
    set.add(new String(test1.getString("sample1", null)));

    Log.v("Main", "set.size: " + set.size());

    final SharedPreferences.Editor editor2 = test2.edit();
    editor2.clear();
    editor2.putStringSet("sample2", set);
    editor2.apply();
}

运行代码3次会导致:

V/Main﹕ ********
V/Main﹕ set.size: 0
V/Main﹕ This is a test
V/Main﹕ set.size: 1
V/Main﹕ ********
V/Main﹕ set.size: 1
V/Main﹕ This is a test
V/Main﹕ set.size: 1
V/Main﹕ ********
V/Main﹕ set.size: 1
V/Main﹕ This is a test
V/Main﹕ set.size: 1

预期的结果是每次调用都会增加第二个首选项,因为我们正在添加Set eg。

V/Main﹕ ********
V/Main﹕ set.size: 0
V/Main﹕ This is a test
V/Main﹕ set.size: 1
V/Main﹕ ********
V/Main﹕ set.size: 1
V/Main﹕ This is a test
V/Main﹕ set.size: 2
V/Main﹕ ********
V/Main﹕ set.size: 2
V/Main﹕ This is a test
V/Main﹕ set.size: 3

我已经通过getStringSet复制了来自第二个首选项的Set,以避免指向同一个对象。

问题在于Set.add(getString())。如果我使用普通的字符串,例如Set.add(&#34; text&#34;),它会工作。我试图创建另一个字符串副本,例如set.add(new String(getString ....但是这也不起作用。

我已经尝试过清理编辑器或使用commit()而不是apply()而没有运气。

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

好吧,我知道到底发生了什么! 设置基本上不会允许冗余!请阅读documentation。它说

  

将指定的对象添加到此集合中。如果已经包含对象,则不会修改该集。

因此,如果您尝试添加已存在的同一对象,则不会对其进行修改。

您需要添加已经存在的新对象!所以我尝试了这段代码,它对我有用!

void test(int count) //count is nothing but number of iteration of loop
{
    Log.v("Main", "********");

    final SharedPreferences test1 = getSharedPreferences("test1", MODE_PRIVATE);
    final SharedPreferences.Editor editor1 = test1.edit();
    editor1.clear();
    editor1.putString("sample1", "This is a test");
    editor1.apply();

    final SharedPreferences test2 = getSharedPreferences("test2", MODE_PRIVATE);
    final Set<String> set = test2.getStringSet("sample2", new HashSet<String>()); // Edited this line (Removed new HashSet<>) If you do then it creates new object each time. 

    Log.v("Main", "set.size: " + set.size());

    Log.v("Main", test1.getString("sample1", null));

    set.add(test1.getString("sample1" + " " + count, null) + " --- " + count); // along with the text I'm adding count so the object (string) are not same

    Log.v("Main", "set.size: " + set.size());

    final SharedPreferences.Editor editor2 = test2.edit();
    editor2.clear();
    editor2.putStringSet("sample2", set);
    editor2.apply();
}

输出是:

V/Main﹕ ********
V/Main﹕ set.size: 0
V/Main﹕ This is a test
V/Main﹕ set.size: 1
V/Main﹕ ********
V/Main﹕ set.size: 1
This is a test
V/Main﹕ set.size: 2
V/Main﹕ ********
V/Main﹕ set.size: 2
V/Main﹕ This is a test
V/Main﹕ set.size: 3
V/Main﹕ ********
V/Main﹕ set.size: 3
V/Main﹕ This is a test
V/Main﹕ set.size: 4
V/Main﹕ ********
V/Main﹕ set.size: 4
V/Main﹕ This is a test
V/Main﹕ set.size: 5

并替换

final HashSet set = new HashSet<>(test2.getStringSet("sample2", new HashSet<String>()));

final HashSet set = test2.getStringSet("sample2", new HashSet<String>());