I am able to save Strings in my saved preferences but having difficulty saving radio buttons.
public class PersonalDetailsf extends Activity {
private SharedPreferences sharedPreferences;
private RadioGroup radioGroup;
private RadioButton radioSexButton;
private RadioButton rdoMale;
Here is my on Create:
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = loadSavedPreference("name");
strAge = loadSavedPreference("strAge");
strHeight = loadSavedPreference("strHeight");
strWeight = loadSavedPreference("strWeight");
etName.setText(name);
etAge.setText(strAge);
etHeight.setText(strHeight);
etWeight.setText(strWeight);
This is in my onCLick behind a button, where I'm using the radio button and saving the strings:
Name = etName.getText().toString();
age = (int) Double.parseDouble(etAge.getText().toString());
height = (int) Double.parseDouble(etHeight.getText().toString());
weight = (int) Double.parseDouble(etWeight.getText().toString());
int selectedId = radioGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
rdoMale = (RadioButton) findViewById(R.id.rdoMale);
if(rdoMale.isChecked())
{
BMR = 10 * weight + 6.25 * height - 5 * age + 5;
}
else
{
BMR = 10 * weight + 6.25 * height - 5 * age -161;
}
//Save Preferences
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = etName.getText().toString();
savePreference("name",name);
strAge = etAge.getText().toString();
savePreference("strAge",strAge);
strHeight = etHeight.getText().toString();
savePreference("strHeight",strHeight);
strWeight = etWeight.getText().toString();
savePreference("strWeight",strWeight);
Best Answer
Create both a save and a load method:
Save method
Load method
So to save your buttonstates, just call
saveRadioButtons()
And to load your buttonstates back, just call
loadRadioButtons()
somewhere in your code, like youronCreate()
Hope this will help u out