访问共享首选项无法正常工作
Accessing shared preferences not working correctly
我有一段代码可以为列表视图中的特定项目存储 "toggle"。我使用项目的名称作为键。但是,getBoolean 的结果总是returns 第二个参数中指定的默认值。我真的搞不清楚是我实施错了,还是我忽略了什么
为澄清起见,summonerNames 是一个字符串数组列表。
MenuItem toggle = menu.findItem(R.id.postGameNotif);
SharedPreferences prefs = getApplicationContext().getSharedPreferences("summoner_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Log.i(TAG, summonerNames.get(position));
boolean postNotif = prefs.getBoolean(summonerNames.get(position),false);
if (postNotif == true) {
toggle.setTitle("Disable post-game notifications");
Log.i(TAG,"Disabled");
editor.putBoolean(summonerNames.get(position), false);
}
else {
toggle.setTitle("Enable post-game notifications");
Log.i(TAG, "Enabled");
editor.putBoolean(summonerNames.get(position), true);
Log.i(TAG, String.valueOf(prefs.getBoolean(summonerNames.get(position),false)));
}
问题是添加的值未在共享首选项中提交。
添加布尔值后,您错过了在 sharedpreference 中提交更改
勾选thislink
MenuItem toggle = menu.findItem(R.id.postGameNotif);
SharedPreferences prefs = getApplicationContext().getSharedPreferences("summoner_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Log.i(TAG, summonerNames.get(position));
boolean postNotif = prefs.getBoolean(summonerNames.get(position),false);
if (postNotif == true) {
toggle.setTitle("Disable post-game notifications");
Log.i(TAG,"Disabled");
editor.putBoolean(summonerNames.get(position), false);
editor.commit()// you need to commit after adding it to sharedpref
}
else {
toggle.setTitle("Enable post-game notifications");
Log.i(TAG, "Enabled");
editor.putBoolean(summonerNames.get(position), true);
editor.commit()// you need to commit after adding it to sharedpref
Log.i(TAG, String.valueOf(prefs.getBoolean(summonerNames.get(position),false)));
}
我有一段代码可以为列表视图中的特定项目存储 "toggle"。我使用项目的名称作为键。但是,getBoolean 的结果总是returns 第二个参数中指定的默认值。我真的搞不清楚是我实施错了,还是我忽略了什么
为澄清起见,summonerNames 是一个字符串数组列表。
MenuItem toggle = menu.findItem(R.id.postGameNotif);
SharedPreferences prefs = getApplicationContext().getSharedPreferences("summoner_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Log.i(TAG, summonerNames.get(position));
boolean postNotif = prefs.getBoolean(summonerNames.get(position),false);
if (postNotif == true) {
toggle.setTitle("Disable post-game notifications");
Log.i(TAG,"Disabled");
editor.putBoolean(summonerNames.get(position), false);
}
else {
toggle.setTitle("Enable post-game notifications");
Log.i(TAG, "Enabled");
editor.putBoolean(summonerNames.get(position), true);
Log.i(TAG, String.valueOf(prefs.getBoolean(summonerNames.get(position),false)));
}
问题是添加的值未在共享首选项中提交。
添加布尔值后,您错过了在 sharedpreference 中提交更改
勾选thislink
MenuItem toggle = menu.findItem(R.id.postGameNotif);
SharedPreferences prefs = getApplicationContext().getSharedPreferences("summoner_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Log.i(TAG, summonerNames.get(position));
boolean postNotif = prefs.getBoolean(summonerNames.get(position),false);
if (postNotif == true) {
toggle.setTitle("Disable post-game notifications");
Log.i(TAG,"Disabled");
editor.putBoolean(summonerNames.get(position), false);
editor.commit()// you need to commit after adding it to sharedpref
}
else {
toggle.setTitle("Enable post-game notifications");
Log.i(TAG, "Enabled");
editor.putBoolean(summonerNames.get(position), true);
editor.commit()// you need to commit after adding it to sharedpref
Log.i(TAG, String.valueOf(prefs.getBoolean(summonerNames.get(position),false)));
}