如何让 SharedPreferences 更新数据而不是覆盖数据并丢失部分数据? [英] How can I make SharedPreferences to update the data instead of overwriting the data and losing part of it?
问题描述
我使用以下代码将 ArrayList
保存到 SharedPreferences
中:
StringBuilder stringBuilder = new StringBuilder();for(字符串字符串:学期数组){stringBuilder.append(str);stringBuilder.append(",");}SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES", MODE_PRIVATE);SharedPreferences.Editor 编辑器 = sharedPreferences.edit();editor.putString(semesterArray", stringBuilder.toString());editor.apply();
这在一定程度上完成了工作,因为它按预期保存了 ArrayList
,并且当应用程序重新启动时,添加的 Semesters
仍然存储在 ArrayList
中,正如我希望的那样.但是,如果我在重新启动应用程序后添加更多 Semesters
,保存的 Semesters
将被覆盖,并且我会丢失之前保存的 Semesters
.有没有办法更新 SharedPreferences
而不是覆盖?如果没有,我应该朝哪个方向存储这些 Semesters
?感谢您的帮助!
正如我在评论中提到的,使用 sharedPrefences 来更新或插入新值不是一个好的做法,但如果你想使用 sharedPrefences,我会给你一个解决方案
你能做的是:
- 创建名为 current 的优先级以保存字符串的值
- 创建另一个名为 new 的偏好,以在之后保存新数据检查它是否不等于当前.
你会以这样的代码结束:
sharedPreferences = getSharedPreferences(shared",MODE_PRIVATE);编辑器 = sharedPreferences.edit();String text = "第一个文本";editor.putString(当前",文本);if (!sharedPreferences.getString("current","").equals(sharedPreferences.getString("new",""))){editor.putString("new",sharedPreferences.getString("current","") + text );editor.commit();}editor.commit();
<块引用>
第一次保存后的结果:sharedPreferences.getString("current","")将返回一些文本"和 sharedPreferences.getString("new","") 将返回"
更改第一个文本"后的结果到新文本":sharedPreferences.getString("current","") 将返回新文本";和sharedPreferences.getString("new","") 将返回 "first text new"文字"
如何使用:
如果 sharedPreferences.getString("new","") 等于 ""您的数据未更新使用 sharedPreferences.getString("current","")否则您的数据已更新,因此请使用 sharedPreferences.getString("new","")
I'm using the following piece of code to save an ArrayList<String>
into SharedPreferences
:
StringBuilder stringBuilder = new StringBuilder();
for(String str: semesterArray) {
stringBuilder.append(str);
stringBuilder.append(",");
}
SharedPreferences sharedPreferences = getSharedPreferences("PREFERENCES", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("semesterArray", stringBuilder.toString());
editor.apply();
This is somewhat getting the job done since it saves the ArrayList<String>
as intended and when the app is re-launched the Semesters
that were added are still stored in the ArrayList<String>
as I want them to be. But if I add more Semesters
after re-launching the app, the Semesters
that were saved are overwritten and I lose the previously saved Semesters
. Is there a way SharedPreferences
can be updated instead of overwritten? If not, in what direction should I move to store these Semesters
? Thanks for the help!
As i mentioned in my comment using sharedPrefences to update or insert new values is not a good practice , but i will give you a solution if you want to use sharedPrefences
what you can do is :
- create prefrence called current to save the value of your string
- create another prefrence called new , to save the new data after checking that it's not equal to the current.
you will end with something like this code :
sharedPreferences = getSharedPreferences("shared",MODE_PRIVATE);
editor = sharedPreferences.edit();
String text = "first text";
editor.putString("current",text);
if (!sharedPreferences.getString("current","").equals(sharedPreferences.getString("new","")))
{
editor.putString("new",sharedPreferences.getString("current","") + text );
editor.commit();
}
editor.commit();
Result after first save : sharedPreferences.getString("current","") will return "some text" and sharedPreferences.getString("new","") will return ""
Result after changing "first Text" to " new text" : sharedPreferences.getString("current","") will return "new text" and sharedPreferences.getString("new","") will return "first text new text"
how to use it :
if sharedPreferences.getString("new","") equals ""
your data is not updated use sharedPreferences.getString("current","")
else your data is updated so use sharedPreferences.getString("new","")
这篇关于如何让 SharedPreferences 更新数据而不是覆盖数据并丢失部分数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!