lundi 29 juin 2015

Using sharedpreferences for saving a lot of checkbox states

I know there is a lot of documentation for using shared preferences but I confused on how to implement it for my case. I have custom action bar for my activities with custom buttons that take you to the previous and next pages. When I navigate via these buttons, I want to be able to save the state of all of my checkboxes. I'm 90% sure I'm doing this wrong and I wonder if there is a better way considering the amount of lines I'm using. This code is in my first activity and when I clicked my custom next button and then previous button to return to it, it's not saving state:

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);

}




Aucun commentaire:

Enregistrer un commentaire