Skip to content Skip to sidebar Skip to footer

Spinner With Sharedpreferences

Good evening. I'm trying to save the choice the user makes in Spinner in SharedPreferences, when he clicks OK, but I'm not getting it, can anyone help me? Then I want to redeem the

Solution 1:

check this,

publicclassPreferenceManager {
privateContext context;
privateSharedPreferences sharedPreferences;
privateSharedPreferences.Editor editor;


publicPreferenceManager(Context context) {
    this.context = context;
    sharedPreferences = context.getSharedPreferences("General", Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();
    editor.apply();
}

publicvoidsaveString(String key, String value) {
    editor.putString(key, value);
    editor.commit();
}

publicStringgetString(String key) {
    return sharedPreferences.getString(key, "NA");
}
}

to save the string

PreferenceManagerpreferenceManager=newPreferenceManager(context);
preferenceManager.saveString("key","StringValue");

get the stored string like this

PreferenceManagerpreferenceManager=newPreferenceManager(context);
 StringsavedString= preferenceManager.getString("key");

Post a Comment for "Spinner With Sharedpreferences"