行为不端尝试存储使用共享preferences串集时 [英] Misbehavior when trying to store a string set using SharedPreferences
问题描述
我想用来存储一组字符串的<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.html"><$c$c>Shared$p$pferences$c$c> API 。
I'm trying to store a set of strings using the SharedPreferences
API.
Set<String> s = sharedPrefs.getStringSet("key", new HashSet<String>());
s.add(new_element);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(s);
edit.commit()
我第一次执行code以上,取值
设置为默认值(刚才创建的末尾空的HashSet
),它被存储没有问题。
The first time I execute the code above, s
is set to the default value (the just created end empty HashSet
) and it is stored without problems.
第二,接下来的时间我执行此code,A 取值
对象与第一个元素返回补充说。我可以添加的元素,并在程序执行过程中,它显然是存储在共享preferences
,但是当程序被杀害,在共享preferences
从它的持久存储再读一遍,新的值都将丢失。
The second and next times I execute this code, a s
object is returned with the first element added. I can add the element, and during the program execution, it is apparently stored in the SharedPreferences
, but when the program is killed, the SharedPreferences
read again from its persistent storage and the newer values are lost.
如何才能在这之后,储存,使他们不会迷路?第二,和要素
How can the second, and elements after that, be stored so they won't get lost?
推荐答案
这个问题被记录在<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.html#getStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29"><$c$c>Shared$p$pferences.getStringSet$c$c>.
This "problem" is documented on SharedPreferences.getStringSet
.
在共享preferences.getStringSet
返回存储HashSet的对象的引用
在共享preferences里面
。当您添加元素到这个对象,他们的加入实际上共享preferences里面
。
The SharedPreferences.getStringSet
returns a reference of the stored HashSet object
inside the SharedPreferences
. When you add elements to this object, they are added in fact inside the SharedPreferences
.
这是确定的,但是当你试图存储它的问题就来了:Android的比较,你要保存使用修改后的HashSet 共享preferences.Editor.putStringSet
与当前存储在共享preference
,两者是同一个对象!
That is ok, but the problem comes when you try to store it: Android compares the modified HashSet that you are trying to save using SharedPreferences.Editor.putStringSet
with the current one stored on the SharedPreference
, and both are the same object!!!
一个可能的解决方案是让设置℃的复印件;字符串&GT;
的共享preferences返回
目标:
A possible solution is to make a copy of the Set<String>
returned by the SharedPreferences
object:
Set<String> s = new HashSet<String>(sharedPrefs.getStringSet("key", new HashSet<String>()));
这使得取值
不同的对象,并将其添加到取值
将不会添加到集合中的字符串存储在共享preferences里面
。
That makes s
a different object, and the strings added to s
will not be added to the set stored inside the SharedPreferences
.
其他的解决办法,将工作就是使用相同的共享preferences.Editor
交易来存储另一种更简单preference(如整数或布尔)你唯一需要的是迫使该储存的值都在每一笔交易(例如,你可以存储字符串集大小)不同。
Other workaround that will work is to use the same SharedPreferences.Editor
transaction to store another simpler preference (like an integer or boolean), the only thing you need is to force that the stored value are different on each transaction (for example, you could store the string set size).
这篇关于行为不端尝试存储使用共享preferences串集时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!