Android Store Radiogroup Id In Sharedpreferences And Load It
In my App i want to open a dialog window with a radiogroup with some items (each item should be an activity ) the user can choose from. The chosen item/ID should get stored in the
Solution 1:
There are many samples but ok, I'll give an example:
You can define 2 methods under your activity:
privatevoidloadSavedPreferences() {
SharedPreferencessharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
intselectedRadioID= sharedPreferences.getInt("SELECTED_RADIO", 0);
if(selectedRadioID > 0) {
// you got previously selected radioRadioButtonrb= (RadioButton)findViewById(selectedRadioID);
rb.setSelected(true);
}
}
privatevoidsavePreferences(String key, int radioId) {
SharedPreferencessharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
Editoreditor= sharedPreferences.edit();
editor.putInt(key, radioId);
editor.commit();
}
Use this methods on your activities onCreate
method.
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadSavedPreferences();
RadioGrouprg= findViewById(R.id.your_radio_group_over_your_radios);
rg.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(RadioGroup group, int checkedId) {
savePreferences("SELECTED_RADIO", checkedId);
}
});
}
You should improve this code, but this will give you the idea.
Post a Comment for "Android Store Radiogroup Id In Sharedpreferences And Load It"