尝试使用 SharedPreferences 存储字符串集时的不当行为 [英] Misbehavior when trying to store a string set using SharedPreferences

查看:27
本文介绍了尝试使用 SharedPreferences 存储字符串集时的不当行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SharedPreferences 存储一组字符串 API.

Sets = sharedPrefs.getStringSet("key", new HashSet());s.add(new_element);SharedPreferences.Editor 编辑器 = sharedPrefs.edit();editor.putStringSet(s);编辑提交()

第一次执行上面的代码时,s被设置为默认值(刚刚创建的结束为空HashSet),存储没有问题.>

第二次和下一次我执行这段代码时,会返回一个 s 对象,并添加了第一个元素.我可以添加元素,在程序执行期间,它显然存储在 SharedPreferences 中,但是当程序被终止时,SharedPreferences 再次从其持久存储中读取并较新的值丢失了.

如何存储第二个以及之后的元素以免丢失?

解决方案

这个问题"记录在 SharedPreferences.getStringSet.

SharedPreferences.getStringSet 返回存储的 HashSet 对象的引用SharedPreferences 里面.当您向该对象添加元素时,它们实际上被添加到 SharedPreferences 中.

没关系,但是当您尝试存储它时会出现问题:Android 将您尝试使用 SharedPreferences.Editor.putStringSet 保存的修改后的 HashSet 与存储在 SharedPreference,而且都是同一个对象!!!

一种可能的解决方案是制作由 SharedPreferences 对象返回的 Set 的副本:

Sets = new HashSet(sharedPrefs.getStringSet("key", new HashSet()));

这使得 s 成为不同的对象,并且添加到 s 的字符串不会添加到存储在 SharedPreferences 中的集合中.

其他可行的解决方法是使用相同的 SharedPreferences.Editor 事务来存储另一个更简单的首选项(如整数或布尔值),您唯一需要的是强制存储的值是每个事务都不同(例如,您可以存储字符串集大小).

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.putStringSet(s);
edit.commit()

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.

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?

解决方案

This "problem" is documented on SharedPreferences.getStringSet.

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.

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!!!

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>()));

That makes s a different object, and the strings added to s will not be added to the set stored inside the SharedPreferences.

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).

这篇关于尝试使用 SharedPreferences 存储字符串集时的不当行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆