공유 환경 설정 값을 변경했지만 여전히 이전 환경 값을 반환합니다. 내가 뭘 놓치고 있니?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'를 저장해야합니다. 값을 토글합니다. –