Hobbyists and spinners in general preferences?

I have two RadioButtons for Gender in RadioGroup

1) Man

2) Female

and I want to save it in shared preferences and get it when I open it again.

Like I want to save the spinner in sharedpreferences, how can I save it.

0
source share
1 answer
// Try this way,hope this will help you... activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <RadioGroup android:id="@+id/rdgGender" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/rdbMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male"/> <RadioButton android:id="@+id/rdbFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female"/> </RadioGroup> </LinearLayout> MainActivity.java public class MainActivity extends Activity{ private RadioGroup rdgGender; private RadioButton rdbMale; private RadioButton rdbFemale; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rdgGender = (RadioGroup) findViewById(R.id.rdgGender); rdbMale = (RadioButton) findViewById(R.id.rdbMale); rdbFemale = (RadioButton) findViewById(R.id.rdbFemale); rdgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); switch (checkedId){ case R.id.rdbMale: editor.putInt("genderValue", R.id.rdbMale); break; case R.id.rdbFemale: editor.putInt("genderValue",R.id.rdbFemale); break; } editor.commit(); } }); SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE); switch (sharedPreferences.getInt("genderValue",R.id.rdbMale)){ case R.id.rdbMale: rdbMale.setChecked(true); break; case R.id.rdbFemale: rdbFemale.setChecked(true); break; } } } 
0
source

Source: https://habr.com/ru/post/970996/


All Articles