如何使SharedPreferences更新数据,而不是覆盖数据并丢失部分数据? [英] How can I make SharedPreferences to update the data instead of overwriting the data and losing part of it?
问题描述
我正在使用以下代码将ArrayList<String>
保存到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();
这有点可以完成工作,因为它按预期方式保存了ArrayList<String>
,并且当应用重新启动时,添加的Semesters
仍按我希望的方式存储在ArrayList<String>
中.但是,如果在重新启动应用程序后添加更多Semesters
,则保存的Semesters
将被覆盖,并且我将丢失之前保存的Semesters
.有没有一种方法可以更新SharedPreferences
而不是将其覆盖?如果不是,我应该朝哪个方向存储这些Semesters
?感谢您的帮助!
正如我在评论中提到的,使用sharedPrefences更新或插入新值不是一个好习惯,但是如果您要使用sharedPrefences,我会给您一个解决方案
您可以做的是:
- 创建称为当前"的首选项以保存字符串的值
- 创建另一个名为new的首选项,以在之后保存新数据 检查它是否不等于当前值.
您将以以下代码结尾:
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();
首次保存后的结果:sharedPreferences.getString("current",") 将返回某些文本"和sharedPreferences.getString("new",")将 返回"
更改第一个文本"后的结果新文本" : sharedPreferences.getString("current",")将返回"new text".和 sharedPreferences.getString("new",")将返回第一个新的文本" 文字"
如何使用它:
if sharedPreferences.getString("new","") equals ""
your data is not updated use sharedPreferences.getString("current","")
else your data is updated so use 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屋!