-3

I using sharedPrefrence in my app but it's not return any value this is code :

 public class SharedPrefManager {
    public final String MY_PREFS_NAME = "name";
    private Context context;
    SharedPreferences sp;
    public SharedPrefManager(Context context){
        this.context = context;
        sp = context.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
        Log.d(TAG, "database is created");
    }

    public void saveInfoUser(String username ,int numRow){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString("username", username);
        editor.putInt("numberRow",numRow);
        editor.commit();
    }


    public String getUsename(){


        String username = sp.getString("username","");
        return username;
    }
    public void logout(){
        sp.edit().clear().commit();
    }

}

but when call method getUsename() return "" and not return values

this is place of called method : enter image description here

ramtin m
  • 13
  • 5
  • Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – azurefrog Oct 23 '18 at 16:45
  • @azurefrog not working ! – ramtin m Oct 23 '18 at 16:56
  • Well your code is a bit odd... Like why are you creating two sharedPreference instances in your constructor? Either way, I don't see any glaring issues. Are you saying it returns empty string? Also you should move your keys to constants to be sure consistent read / writes. – Sam Oct 23 '18 at 16:59
  • @sam my code changed, no in debug sharedpreferenceslmpl@6227 – ramtin m Oct 23 '18 at 17:05
  • supplied you an answer – Sam Oct 23 '18 at 17:10

2 Answers2

0

Make sure you are using the same shared preference for saving as you are retrieving.

Your read is using private shared pref by key MY_PREFS_NAME

but your write is using default shared pref with no key.

enter image description here

Sam
  • 5,342
  • 1
  • 23
  • 39
  • no problem, as in, you are good now? or as in you will make the adjustment and you are still having the issue? – Sam Oct 23 '18 at 17:12
  • please check my answer! – ramtin m Oct 23 '18 at 17:23
  • Have you put a breakpoint and confirmed you are not passing "" blank into the save routine? – Sam Oct 23 '18 at 17:43
  • I think you may be confused by my question. Otherwise you already know the answer. I asked if you are passing in a blank string when you call the save method. If the answer is "YES" then obviously you would read back a blank string because that is what you saved in there. – Sam Oct 23 '18 at 17:45
0

The critical part of the code is this:

SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();

That line of code should be this:

    SharedPreferences.Editor editor = sp.edit();

Once you obtain the SharedPreferences object in your class constructor you should use it to create the editor.

 public class SharedPrefManager {
    public final String MY_PREFS_NAME = "name";
    private final String MY_PREFS_USERNAME = "username";
    private final String MY_PREFS_NUMBER_ROW = "numberRow";

    SharedPreferences sp;

    public SharedPrefManager(Context context){
        sp = context.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
        Log.d(TAG, "database is created");
    }

    public void saveInfoUser(String username ,int numRow){
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(MY_PREFS_USERNAME , username);
        editor.putInt(MY_PREFS_NUMBER_ROW ,numRow);
        editor.commit();
    }


    public String getUsename(){
        String username = sp.getString(MY_PREFS_USERNAME ,"");
        return username;
    }
    public void logout(){
        sp.edit().clear().commit();
    }

}
Barns
  • 4,850
  • 3
  • 17
  • 31