我正在使用DefaultSharedPrefeneces来保存和加载导航抽屉中复选框的状态。
创建导航抽屉并从DefaultPreferences加载复选框的状态时,如果每个复选框的布尔值为true,则复选框设置为选中。
当用户点击复选框时,状态会被保存到defaultsharedPrefences。 我对这一切都没有问题。
问题是,然后我尝试在提交之前将2个布尔值放入defaultsharedprefences,它不起作用并且似乎覆盖了第一个布尔值。
这是我的代码
//Navigation Drawer
private void addDrawerItems() {
String[] osArray = {"item1", "item2", "item3"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);
if (mIsPremiumUser) {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
} else {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
mDrawerList.setAdapter(mAdapter);
Boolean isCheckedValue;
// *** THIS IS WHERE I LOAD THE CHECKBOX STATE FROM DEFAULTSHAREDPREFERENCES ***
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
isCheckedValue = preferences.getBoolean("cbox1", false);
mDrawerList.setItemChecked(0, isCheckedValue);
isCheckedValue = preferences.getBoolean("cbox2", true);
mDrawerList.setItemChecked(1, isCheckedValue);
isCheckedValue = preferences.getBoolean("cbox3", true);
mDrawerList.setItemChecked(2, isCheckedValue);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
if (!mIsPremiumUser) {
Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
return;
}
switch (position) {
case 0:
if (ctv.isChecked()) {
requestActivityUpdates();
Toast.makeText(getApplicationContext(), "item1ON", Toast.LENGTH_SHORT).show();
// THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox1", ctv.isChecked());
editor.commit();
} else {
removeActivityUpdates();
Toast.makeText(getApplicationContext(), "item1OFF", Toast.LENGTH_SHORT).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox1", false);
editor.commit();
}
break;
case 1:
if (ctv.isChecked()) {
Toast.makeText(getApplicationContext(), "item2 ON", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
// THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
editor.putBoolean("cbox2", ctv.isChecked());
editor.putBoolean("callStatus", ctv.isChecked()); // << THIS LINE IS CAUSING ME PROBLEMS
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "item2 OFF", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox2", ctv.isChecked());
editor.putBoolean("callStatus", ctv.isChecked());
editor.commit();
}
break;
case 2:
if (ctv.isChecked()) {
Toast.makeText(getApplicationContext(), "item3 ON", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox3", ctv.isChecked());
editor.putBoolean("smsStatus", ctv.isChecked());
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "item3 OFF", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox3", ctv.isChecked());
editor.putBoolean("smsStatus", ctv.isChecked());
editor.commit();
}
break;
}
}
});
}
我确信它在每个单击的项目上都适用于一个putBoolean,但是当我在editor.com之前在同一个if / else中有两个putBoolean时却没有。 关于什么导致问题以及如何解决问题的任何建议?非常感谢它。
答案 0 :(得分:0)
我明白了! 因为布尔值存储在外部defaultSharedPrefenences文件中, 在应用程序之外,值始终从中读入。 我最后一次点击复选框就会拉出状态。 我在想,每当我强行关闭应用程序时,它都会回到我想要的默认选择状态,这是错误的,真实的,真实的。但由于默认的Sharedpreferences在应用程序未被销毁时会以最新状态加载。
我必须在应用程序管理器中清除应用程序数据,以便将布尔值设置为null,并显示false,true,true。