英文:
SharedPreferences change set values after apply()
问题
Variable declaration
public String am_PM1, am_PM2;
public static final String AM_PM1 = "";
public static final String AM_PM2 = "";
I have 2 string values that are being put in sharedPreferences, but after they get put in and applied, the second value in this case AM_PM2
overwrites AM_PM1
. If I change the order of execution and put AM_PM2
before AM_PM1
, then AM_PM1
overwrites the first value.
Log.d("lol", "Value before 1: " + am_PM1);
Log.d("lol", "Value before 2: " + am_PM2);
editor.putString(AM_PM1, am_PM1);
editor.putString(AM_PM2, am_PM2);
editor.apply();
Log.d("lol", "" + sharedPreferences.getAll());
Log.d("lol", "Value after 1: " + sharedPreferences.getString(AM_PM1, ""));
Log.d("lol", "Value after 2: " + sharedPreferences.getString(AM_PM2, ""));
Screenshot of the console output:
英文:
Variable declaration
public String am_PM1, am_PM2;
public static final String AM_PM1 = "";
public static final String AM_PM2 = "";
I have 2 string values that are being put in sharedPreferences, but after they get put in and applied, the second value in this case AM_PM2
overwrites AM_PM1
. If I change the order of execution and put AM_PM2
before AM_PM1
, then AM_PM1
overwrites the first value.
Log.d("lol", "Value before 1: " + am_PM1);
Log.d("lol", "Value before 2: " + am_PM2);
editor.putString(AM_PM1, am_PM1);
editor.putString(AM_PM2, am_PM2);
editor.apply();
Log.d("lol", "" + sharedPreferences.getAll());
Log.d("lol", "Value after 1: " + sharedPreferences.getString(AM_PM1, ""));
Log.d("lol", "Value after 2: " + sharedPreferences.getString(AM_PM2, ""));
Screenshot of the console output:
答案1
得分: 0
在第一次尝试时,我没有意识到分配给变量的实际字符串值很重要。所以,如果你遇到这样的问题,请确保你不像我这样傻,并确实为变量分配了一个字符串值。
public static final String AM_PM1 = "上午下午1";
public static final String AM_PM2 = "上午下午2";
英文:
Doing this for the first time, I didn't realize that the actual string value assigned to the variable mattered. So if you encounter such a problem make sure that you are not as silly as me and have actually assigned a string value to the variable.
public static final String AM_PM1 = "amPM1";
public static final String AM_PM2 = "amPM2";
答案2
得分: 0
SharedPreferences
使用键值对来存储数据。如果您的键相同,这意味着每次写入一个值时,它会覆盖先前的值。因此,在尝试检索它们时可能会得到意外的值。
英文:
SharedPreferences
use a Key-Value pair to store data. If your keys are the same, it means every time you write a value, you over write the previous one. Thus giving you unexpected values when you try to retrieve them.
答案3
得分: 0
AM_PM1 和 AM_PM2 相等。删除 public static final String AM_PM1 = "";
和 public static final String AM_PM2 = "";
,然后执行以下操作:
editor.edit().putString("AM_PM1", am_PM1).apply();
editor.edit().putString("AM_PM2", am_PM2).apply();
英文:
AM_PM1 & AM_PM2 are equal. remove public static final String AM_PM1 = "";
& public static final String AM_PM2 = "";
do this :
editor.edit().putString("AM_PM1", am_PM1).apply();
editor.edit().putString("AM_PM2", am_PM2).apply();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论