Question

I have following code:

This is in onCreate - I am trying to set default value of a radio button here, which will be the pre selected value on radio button.

pref = getSharedPreferences(Constants.PREF_SETTINGS, 0);                            

        RadioGroup rg_numberOfQuestions = (RadioGroup) findViewById(R.id.radioGroupQuestions);
        int selectedOption = rg_numberOfQuestions.getCheckedRadioButtonId();    
        rb_NumberOfQuestions = (RadioButton) findViewById(selectedOption);      
        rg_numberOfQuestions.setOnCheckedChangeListener(this);      

        final RadioGroup rg_numbersBetween = (RadioGroup) findViewById(R.id.radioGroupNumbersBetween);
        selectedOption = rg_numbersBetween.getCheckedRadioButtonId();   
        rb_NumbersBetween = (RadioButton) findViewById(selectedOption);
        rg_numbersBetween.setOnCheckedChangeListener(this);

@Override
    public void onCheckedChanged(RadioGroup rg, int i) {                        
        switch (rg.getCheckedRadioButtonId())
        {
        case R.id.rb_0to10:
            savePref("rb_0to10", rg.getCheckedRadioButtonId());         
            break;

        case R.id.rb_0to25:
            savePref("rb_0to25", rg.getCheckedRadioButtonId());     
            break;

        case R.id.rb_0to50:
            savePref("rb_0to50", rg.getCheckedRadioButtonId());
            break;
        case R.id.rb_15:
            savePref("rb_15", rg.getCheckedRadioButtonId());
            break;

        case R.id.rb_25:
            savePref("rb_25", rg.getCheckedRadioButtonId());
            break;

        case R.id.rb_50:
            savePref("rb_50", rg.getCheckedRadioButtonId());
            break;          
        }       
    }   


private void savePref(String key, int value) {
        pref = getSharedPreferences(Constants.PREF_SETTINGS, 0);
        editor = pref.edit();
        editor.putInt(key, value);      
        editor.commit();
    }

Above code works first time it gets the default value of first radio button. But no matter what other radio button i select it is not working it is not saving the button click value it just gets first radio button always.

Was it helpful?

Solution

You can see the value itself in DDMS :

Here is the image for Debugging and see the values in Shared Pref :

enter image description here

And

enter image description here

you can pull and push files also in any location... To see Values in Shared pref click that file

on Top right side there is 2 icon of mobile with pink Arrow from there u can push and pull Shared pref Files

OTHER TIPS

Try this code of snippet.. It works perfect...

OnCreate Method code :

        pref = getSharedPreferences(Constants.PREF_SETTINGS, 0); 

        rg_numberOfQuestions = (RadioGroup) findViewById(R.id.radioGroup1);
        rb_1 = (RadioButton) findViewById(R.id.radioOne);
        rb_1.setOnCheckedChangeListener(this);


        rb_2 = (RadioButton) findViewById(R.id.radioTwo);
        rb_2.setOnCheckedChangeListener(this);

        rb_3 = (RadioButton) findViewById(R.id.radioThree);
        rb_3.setOnCheckedChangeListener(this);


        int selected_radio_button = pref.getInt("radio", 0);
        if(selected_radio_button!=0)
        {
            RadioButton  button = (RadioButton) findViewById(selected_radio_button);
            button.setChecked(true);
        }

And the listener code :

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked)
    {
        Editor editor = pref.edit();
        editor.putInt("radio", buttonView.getId());
        editor.commit();
    }
}

It seem Initialization of share-preference is not correct. can you just try below code@

private void savePref(String key, int value) {
      pref = getSharedPreferences("ANYNAME",Context.MODE_PRIVATE);
        editor = pref.edit();
        editor.putInt(key, value);      
        editor.commit();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top