Veer3383
Veer3383

Reputation: 1825

why are my shared preference values being overwritten?

I want to store different values on different events in the shared preferences.

eg. i have a toggle button which sets profilevisibility to On and off, so if i check the toggle button, it should store the profilevisibility value as 1.

now i also have another toggle button there which sets matchPreference to on and off, so if i uncheck this toggle button, it should set the matchPreference value as 0.

what happens is, when i set profileVisibility to ON, it does saves the value as 1 but, now if i set the MatchPreference to OFF, it also saves the values as 0, but overwrites other values too..

This is my code called on OnCreate in the Activity

HashMap<String, String> profileVisibility = session.getProfileVisibility();
profilevibility_value =profileVisibility.get(UserSessionManager.KEY_VISIBILITY);

HashMap<String, String> discoverability = session.getDiscoverablity();
discoverability_value = discoverability.get(UserSessionManager.KEY_DISCOVER);

HashMap<String, String> listmatches = session.getListMatches();
listmatches_value = listmatches.get(UserSessionManager.KEY_MATCH);

Values get overwritten for all three variable here, why?

These are my methods for storing and fetching values in my session manager

public static final String KEY_VISIBILITY = "1";
public static final String KEY_DISCOVER = "1";
public static final String KEY_MATCH = "1";
public void StoreProfileVisiblity(String male){

        editor.putString(KEY_VISIBILITY, male);
        editor.commit();
    }
    public void StoreDiscoverablity(String status){

        editor.putString(KEY_DISCOVER, status);
        editor.commit();
    }
    public void StoreListMatches(String match){

        editor.putString(KEY_MATCH, match);
        editor.commit();
    }
    public HashMap<String, String> getProfileVisibility(){

        HashMap<String, String> mpv = new HashMap<String, String>();
        mpv.put(KEY_VISIBILITY, pref.getString(KEY_VISIBILITY, null));
        return mpv;
    }

    public HashMap<String, String> getDiscoverablity(){

        HashMap<String, String> dv = new HashMap<String, String>();
        dv.put(KEY_DISCOVER, pref.getString(KEY_DISCOVER, null));
        return dv;
    }
    public HashMap<String, String> getListMatches(){

        HashMap<String, String> lmv = new HashMap<String, String>();
        lmv.put(KEY_MATCH, pref.getString(KEY_MATCH, null));
        return lmv;
    }

Upvotes: 0

Views: 123

Answers (1)

Kabir
Kabir

Reputation: 1479

Assign different value for each of those keys. Like...

public static final String KEY_VISIBILITY = "KEY_VISIBILITY";
public static final String KEY_DISCOVER = "KEY_DISCOVER";
public static final String KEY_MATCH = "KEY_MATCH";

I think, it will solve your problem.

Upvotes: 1

Related Questions