Search code examples
androidcheckboxandroid-savedstate

Saving multiple checkboxes using saved preferences in android?


i have an app with 7 checkboxes, 1 text feild and a save button..... i want users to be able to select any amount of the checkboxes as they want and type in the text field and click save..... then when they open the app again the correct checkboxes should be checked and the text that they typed should be in the textbox....

ive almost got it working however it only seems to be saving one checkbox at a time or none at all and i cant seem to figure out what i am doing wrong... i have read all the threads and posts and cant seem to get it right....

any help would be appreciated

here is my code

package com.rapeventplannerchecklist.app.rapeventplannerchecklist;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

CheckBox checkBox;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
CheckBox checkBox5;
CheckBox checkBox6;
CheckBox checkBox7;
EditText editText;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkBox = (CheckBox) findViewById(R.id.checkBox1);
    checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
    checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
    checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
    checkBox6 = (CheckBox) findViewById(R.id.checkBox6);
    checkBox7 = (CheckBox) findViewById(R.id.checkBox7);
    editText = (EditText) findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);
    loadSavedPreferences();
}

private void loadSavedPreferences() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
    String name = sharedPreferences.getString("storedName", "YourName");
    if (checkBoxValue) {
        checkBox.setChecked(true);
    } else {
        checkBox.setChecked(false);
    }

    boolean checkBox2Value = sharedPreferences.getBoolean("CheckBox_Value2", false);
    if (checkBox2Value) {
        checkBox2.setChecked(true);
    } else {
        checkBox2.setChecked(false);
    }

    boolean checkBox3Value = sharedPreferences.getBoolean("CheckBox_Value3", false);
    if (checkBox3Value) {
        checkBox3.setChecked(true);
    } else {
        checkBox3.setChecked(false);
    }

    boolean checkBox4Value = sharedPreferences.getBoolean("CheckBox_Value4", false);
    if (checkBox4Value) {
        checkBox4.setChecked(true);
    } else {
        checkBox4.setChecked(false);
    }

    boolean checkBox5Value = sharedPreferences.getBoolean("CheckBox_Value5", false);
    if (checkBox5Value) {
        checkBox5.setChecked(true);
    } else {
        checkBox5.setChecked(false);
    }

    boolean checkBox6Value = sharedPreferences.getBoolean("CheckBox_Value6", false);
    if (checkBox6Value) {
        checkBox6.setChecked(true);
    } else {
        checkBox6.setChecked(false);
    }

    boolean checkBox7Value = sharedPreferences.getBoolean("CheckBox_Value7", false);
    if (checkBox7Value) {
        checkBox7.setChecked(true);
    } else {
        checkBox7.setChecked(false);
    }



    editText.setText(name);
}

private void savePreferences(String key, boolean value) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

private void savePreferences(String key, String value) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    savePreferences("CheckBox_Value", checkBox.isChecked());
    if (checkBox.isChecked())
        savePreferences("CheckBox_Value2", checkBox2.isChecked());
    if (checkBox2.isChecked())
        savePreferences("CheckBox_Value3", checkBox3.isChecked());
    if (checkBox3.isChecked())
        savePreferences("CheckBox_Value4", checkBox4.isChecked());
    if (checkBox4.isChecked())
        savePreferences("CheckBox_Value5", checkBox5.isChecked());
    if (checkBox5.isChecked())
        savePreferences("CheckBox_Value6", checkBox6.isChecked());
    if (checkBox6.isChecked())
        savePreferences("CheckBox_Value7", checkBox7.isChecked());
    if (checkBox7.isChecked())
        savePreferences("storedName", editText.getText().toString());

    finish();
}
}

any help would be much appreciated!!!


Solution

  • Remove your if statement when saving the state, otherwise it is only saving checkboxes that are checked and we don't want that.

    // Remove
    if (checkBox2.isChecked())
        // Without if statement this will save true if checkbox is checked
        // false if it is not
        savePreferences("CheckBox_Value2", checkBox2.isChecked());
    

    Also you can simplify

    boolean checkBox7Value = sharedPreferences.getBoolean("CheckBox_Value7", false);
    checkBox7.setChecked(checkBox7Value);