如何恢复和存储两个String值?

时间:2011-10-03 12:07:39

标签: android

我有两个值,如何存储和恢复它。

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(PatientType, Teethsselected);
    editor.commit();
}

public String getCaseInfo() {
    SharedPreferences settings = getSharedPreferences(DEALSPOTR_PREFS, 0);
    String value = settings.getString(PatientType, Teethsselected);
    return value;
}

是正确的吗?

2 个答案:

答案 0 :(得分:3)

在您的代码中,PatientType不得更改,以便您可以检索Teethsselected

您没有保存 2字符串

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;
}

答案 1 :(得分:0)

您只在此处存储一个值:

editor.putString(PatientType, Teethsselected);

此处PatientType是键,而不是您要保存的值。 同样,您只在这里恢复一个值:

String value = settings.getString(PatientType, Teethsselected);

Teethsselected是密钥PatientType的默认值。如果这是你的意图,那么它是正确的。