使用saveinstancestate保存大量复选框状态

时间:2015-06-29 15:55:05

标签: android checkbox sharedpreferences savestate

我知道有很多关于使用保存实例状态的文档,但我对如何为我的情况实现它感到困惑。我有自定义操作栏,可以使用自定义按钮将您带到上一页和下一页。当我通过这些按钮导航时,我希望能够保存所有复选框的状态。我90%肯定我做错了,我想知道是否有更好的方法来考虑我正在使用的线路数量。这段代码是我的第一个活动,当我点击我的自定义下一个按钮然后上一个按钮返回它时,它没有保存状态:

boolean bPrearrival_1, bPrearrival_2, bPrearrival_3, bPrearrival_4, bPrearrival_5,
        bPrearrival_6, bPrearrival_7, bPrearrival_8, bPrearrival_9, bPrearrival_10,
        bPrearrival_11, bPrearrival_12;

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean("prearrival_1", checkboxList.get(0).isChecked());
    savedInstanceState.putBoolean("prearrival_2", checkboxList.get(1).isChecked());
    savedInstanceState.putBoolean("prearrival_3", checkboxList.get(2).isChecked());
    savedInstanceState.putBoolean("prearrival_4", checkboxList.get(3).isChecked());
    savedInstanceState.putBoolean("prearrival_5", checkboxList.get(4).isChecked());
    savedInstanceState.putBoolean("prearrival_6", checkboxList.get(5).isChecked());
    savedInstanceState.putBoolean("prearrival_7", checkboxList.get(6).isChecked());
    savedInstanceState.putBoolean("prearrival_8", checkboxList.get(7).isChecked());
    savedInstanceState.putBoolean("prearrival_9", checkboxList.get(8).isChecked());
    savedInstanceState.putBoolean("prearrival_10", checkboxList.get(9).isChecked());
    savedInstanceState.putBoolean("prearrival_11", checkboxList.get(10).isChecked());
    savedInstanceState.putBoolean("prearrival_12", checkboxList.get(11).isChecked());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    bPrearrival_1 = savedInstanceState.getBoolean("prearrival_1");
    bPrearrival_2 = savedInstanceState.getBoolean("prearrival_2");
    bPrearrival_3 = savedInstanceState.getBoolean("prearrival_3");
    bPrearrival_4 = savedInstanceState.getBoolean("prearrival_4");
    bPrearrival_5 = savedInstanceState.getBoolean("prearrival_5");
    bPrearrival_6 = savedInstanceState.getBoolean("prearrival_6");
    bPrearrival_7 = savedInstanceState.getBoolean("prearrival_7");
    bPrearrival_8 = savedInstanceState.getBoolean("prearrival_8");
    bPrearrival_9 = savedInstanceState.getBoolean("prearrival_9");
    bPrearrival_10 = savedInstanceState.getBoolean("prearrival_10");
    bPrearrival_11 = savedInstanceState.getBoolean("prearrival_11");
    bPrearrival_12 = savedInstanceState.getBoolean("prearrival_12");
}

@Override
protected void onResume() {
    super.onResume();
    checkboxList.get(0).setChecked(bPrearrival_1);
    checkboxList.get(1).setChecked(bPrearrival_2);
    checkboxList.get(2).setChecked(bPrearrival_3);
    checkboxList.get(3).setChecked(bPrearrival_4);
    checkboxList.get(4).setChecked(bPrearrival_5);
    checkboxList.get(5).setChecked(bPrearrival_6);
    checkboxList.get(6).setChecked(bPrearrival_7);
    checkboxList.get(7).setChecked(bPrearrival_8);
    checkboxList.get(8).setChecked(bPrearrival_9);
    checkboxList.get(9).setChecked(bPrearrival_10);
    checkboxList.get(10).setChecked(bPrearrival_11);
    checkboxList.get(11).setChecked(bPrearrival_12);

}

1 个答案:

答案 0 :(得分:1)

对于更少的代码,我会使用

void saveChecListStateInPreference(){

SharedPreferences preferences =  PreferenceManager.getDefaultSharedPreferences(this);
String PREARRIVAL_PREFIX = "prearrival_";
String checkValue = "";
SharedPreferences.Editor editor =  preferences.edit();

//Total length of your checkboxes
for(int i = 0 ; i< 11 ; i++){
    checkValue = PREARRIVAL_PREFIX + String.valueOf(i+1);//Because it is no zero based where you started
    preferences.putBoolean(checkValue,checkboxList.get(i).isChecked())
}
editor.apply();

}

你可以恢复几乎相同的方式