how to restore and store two String values?
I have two values how can i store and restore it.
public void SetCaseInfo(String PatientType, String Teethsselected) { // All objects are from android.context.Context
SharedPreferences set开发者_如何学Gotings = getSharedPreferences(DEALSPOTR_PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PatientType, Teethsselected);
editor.commit();
}
public String getCaseInfo() {
SharedPreferences settings = getSharedPreferences(DEALSPOTR_PREFS, 0);
String value = settings.getString(PatientType, Teethsselected);
return value;
}
is it correct?
In your code, PatientType
must not change so you can be able to retrieve Teethsselected
You are not saving the 2 strings
public void SetCaseInfo(String PatientType, String Teethsselected) { // All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(DEALSPOTR_PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("teeth", Teethsselected);
editor.putString("patient", PatientType);
editor.commit();
}
public String getTeethsselected() {
SharedPreferences settings = getSharedPreferences(DEALSPOTR_PREFS, 0);
String value = settings.getString("teeth", "default");
return value;
}
public String getPatientType() {
SharedPreferences settings = getSharedPreferences(DEALSPOTR_PREFS, 0);
String value = settings.getString("patient", "default");
return value;
}
You are storing only one value here:
editor.putString(PatientType, Teethsselected);
here PatientType is the key, not the value you want to save. Likewise, you are restoring only one value here:
String value = settings.getString(PatientType, Teethsselected);
Teethsselected
is the default value for the key PatientType
. If it's what you intended, than yes, it is correct.
精彩评论