SharedPreferences 在 apply() 后更改设置值

huangapple go评论76阅读模式
英文:

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:

SharedPreferences 在 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:

SharedPreferences 在 apply() 后更改设置值

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

huangapple
  • 本文由 发表于 2020年8月7日 19:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定