dancingbush
dancingbush

Reputation: 2281

Saving checkbox status as SharedPreference unsuccessful

I am trying to set up a profile where a user selects male or female via a checkbox representing each. I then try to save this data using SharedPreference object. Have added a onClickListener to both checkboxes and if checked a boolean value is set to true.

But no matter what i have tried (and i have tried this for 4 hours!!!!!) when i refresh the activity both boxes are checked and both boolean values are true! Any ideas would be great.

// check box initialisation male and female
        male = (CheckBox) this.findViewById(R.id.cb_MalePrfofile);
        male.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // when male is checked
                if (((CheckBox) v).isChecked()) {
                    sex = "Male";
                    //malePref=true;
                    //femalePref=false;
                    female.setChecked(false);
                    Toast.makeText(getBaseContext(), "malePref=true", Toast.LENGTH_SHORT).show();
                }
            }
        });// end on click for male

        // if female check box is checked
        female = (CheckBox) this.findViewById(R.id.cb_FemalePrfofile);
        female.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // when male is checked
                if (((CheckBox) v).isChecked()) {
                    sex = "Female";
                    //femalePref=true;
                    //malePref=false;
                    male.setChecked(false);
                    Toast.makeText(getBaseContext(), "feamlePref=true", Toast.LENGTH_SHORT).show();
                }
            }// end onClick/if
        });// end on click call for male
    }// end intilize widgets

Setting the SharedPref editor:

//Checkboxes
        if(male.isChecked()){
            //boolean maleValue=true;
        editor.putBoolean("maleValue", true);
        }else if(female.isChecked()){
            //boolean femaleValue=true;
            editor.putBoolean("femaleValue", true);
        }//end else

the onCreate method to set data according to saved preferences:

male.setChecked(prefs.getBoolean("maleValue", false));
female.setChecked(prefs.getBoolean("femaleValue", false));

The XML:

<CheckBox
                android:id="@+id/cb_MalePrfofile"
                android:text="Male"
                android:typeface="serif"
                android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></CheckBox>
               <CheckBox
                android:id="@+id/cb_FemalePrfofile"
                android:layout_width="wrap_content"
                android:text="Female"
                android:typeface="serif"

        android:layout_height="wrap_content"></CheckBox> 

Answering comments: I have the editor.commit() in place already and all other data from spinners, edit texts etc saved fine.

Also have tried adding a Radiogoup but no matter which i select the Male is selected by default overtime activity reloads. Sorry about the long code here but am posting the onCreate method as maybe missing something here:

// TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.profile);
        intilizeWidgets();

        //load teh sahred prefs object
        prefs = this.getSharedPreferences(prefFilename, MODE_PRIVATE);
        //SharedPreferences.Editor editor = prefs.edit();

        //save teh sored values in the Edittexts
        firstName.setText(prefs.getString("firstName", null));
        lastName.setText(prefs.getString("lastName", null));
        insuranceNo.setText(prefs.getString("insuranceNumber", null));
        padiNumber.setText(prefs.getString("padiNum", null));
        aboutMe.setText(prefs.getString("aboutMe", null));

        boolean maleBoo = prefs.getBoolean("maleValue", false);
        Toast.makeText(getBaseContext(), "Male checked is " + maleBoo, Toast.LENGTH_SHORT).show();

        male.setChecked(prefs.getBoolean("maleValue", false));

        female.setChecked(prefs.getBoolean("femaleValue", false));
        boolean femaleBoo = prefs.getBoolean("femaleValue", false);
        Toast.makeText(getBaseContext(), "FeMale checked is " + femaleBoo, Toast.LENGTH_SHORT).show();

        certLevel.setSelection(prefs.getInt("certLevel", 0));
        yearsExperince.setSelection(prefs.getInt("yearsExp", 0));

        //radiobuttons
        isMaleButton.setChecked(prefs.getBoolean("maleButton", false));
        isFemaleButton.setChecked(prefs.getBoolean("femaleButton", false));


    }// end onctreate

When the Save button is clicked I get the Shared pref object and Editor

 public void onClick(View arg0) {
            //get the sahred pref object and its editor to accept values
            this.prefs = this.getSharedPreferences(prefFilename, MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
    if(isMaleButton.isChecked()){
            editor.putBoolean("maleButton", true);
            }else if(isFemaleButton.isChecked()){
            editor.putBoolean("femaleButton", true);


            }

Upvotes: 0

Views: 1155

Answers (1)

Pablo_Cassinerio
Pablo_Cassinerio

Reputation: 887

it seems you're missing editor.commit().

On an unrelated note, why do you use checkboxes? radiobuttons seem more apt

Upvotes: 2

Related Questions