我更改了共享首选项值,但仍返回旧值。我错过了什么?SharedPreferences即使在编辑后也会返回相同的值
此代码在用户单击RecyclerView中的项目时执行。因此,在第一次点击时,我收到了预期的消息" this true"
。但第二次点击我也得到" this true"
,但预计"this false"
。
SharedPreferences prefs = context.getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);
boolean value = prefs.getBoolean(KEY_PREF, true);
if (value) {
Log.v(LOG_TAG, "this true");
Log.v(LOG_TAG, "editing value..");
SharedPreferences.Editor prefs = context.getSharedPreferences(MY_PREF, MODE_PRIVATE).edit();
prefs.putBoolean(KEY_PREF, new_value);
prefs.apply();
} else {
Log.v(LOG_TAG, "this false");
}
'new_value'的值是什么?也许你应该保存'!value'。这将切换值。 –