Skip to content

Instantly share code, notes, and snippets.

@finleyChen
Created April 2, 2013 00:05
Show Gist options
  • Save finleyChen/5288855 to your computer and use it in GitHub Desktop.
Save finleyChen/5288855 to your computer and use it in GitHub Desktop.
save user input in sharedPreference
private void saveUserData() {
Log.d(TAG, "saveUserData()");
// Getting the shared preferences editor
String mKey = getString(R.string.preference_name);
SharedPreferences mPrefs = getSharedPreferences(mKey, MODE_PRIVATE);
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.clear();
// Save email information
mKey = getString(R.string.preference_key_profile_email);
String mValue = (String) ((EditText) findViewById(R.id.editEmail))
.getText().toString();
mEditor.putString(mKey, mValue);
// Read which index the radio is checked.
// edit this out and use as a debug example
// interesting bug because you try and write an int to a string
mKey = getString(R.string.preference_key_profile_gender);
RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.radioGender);
int mIntValue = mRadioGroup.indexOfChild(findViewById(mRadioGroup
.getCheckedRadioButtonId()));
mEditor.putInt(mKey, mIntValue);
// Commit all the changes into the shared preference
mEditor.commit();
Toast.makeText(getApplicationContext(), "saved name: " + mValue,
Toast.LENGTH_SHORT).show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment