-3

I saved my data from an numberDecimal edit text using sharedpreference. When I want to get that data in another activity, it always appears as null, so it wasn't saved correctly. Any suggestion?

This is my code from where I save the data :

public void Start(View view) {
    EditText LevelEdit = (EditText) findViewById(R.id.editText_LevelCurrent);
    String User_Level = LevelEdit.getText().toString();
    if (User_Level == "") {
        Toast.makeText(this, "Select a level",
                Toast.LENGTH_LONG).show();

    } else {


        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(GameConstants.StringvalueOfLevel,User_Level);
        editor.commit();


        Intent myIntent = new Intent(Mygame_Menu.this, Mygame.class);
        startActivity(myIntent);
    }
}
Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
  • Please see this link how to save and get data from `SharedPreferences` https://stackoverflow.com/a/43805317/4097793 – Sukhbir Jul 26 '18 at 06:54
  • Check my answer here https://stackoverflow.com/questions/49765921/spinner-with-sharedpreferences/49766781#49766781 – hasan_shaikh Jul 26 '18 at 07:02

3 Answers3

1

i think your condition is wrong and because of that your saving null object in shared preference don't check the string like this if (User_Level == "")the == operator dose not work on the strings in java ! you should use equals method for comparing to String like this "Amir".equals("Amir")

use this instead if (User_Level==null||User_Level.isEmpty())

Amir Hossein Mirzaei
  • 2,325
  • 1
  • 9
  • 17
0

You are missing to put sharedPreference name as parameter causes error.

Instead of this

 SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();

Change

SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("sharedPreferenceName",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
Mr. Roshan
  • 1,777
  • 13
  • 33
0

You can't compare string value with int format in if loop. Change

if(User_Level == "")

To

if(User_Level.equals(""))

So it will check the condition as correct. Otherwise above will execute else part even if its null.

EDITED

 private static final String PREFS_NAME = "PREF_LEVEL_DATA";
 private static final String PREFS_KEY = "LEVEL";

 public void SaveLevel(Context context, String level) {

    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME,
            Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(PREFS_KEY, level);

    editor.commit();
}

public String RetrieveLevel(Context context) {

    String level;
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME,
            Context.MODE_PRIVATE);

    if (settings.contains(PREFS_KEY)) {
        level = settings.getString(PREFS_KEY, "");
    } else {
        return "";
    }
    return level;
}

For store a value

SaveLevel(getApplicationContext(), User_Level);

For retreive a value

String level = RetreiveLevel(getApplicationContext());

UPDATED

 EditText LevelEdit = (EditText) findViewById(R.id.editText_LevelCurrent);
 String User_Level = LevelEdit.getText().toString();
 if (User_Level.equals("")) {
    Toast.makeText(this, "Select a level",
            Toast.LENGTH_LONG).show();

 } else {


    SharedPreferences sharedPref = getSharedPreferences("ANY_PREFERENCE_NAME",
        Context.MODE_PRIVATE); //Replace "ANY_PREFERENCE_NAME" as your own string
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(GameConstants.StringvalueOfLevel,User_Level);
    editor.commit();


    Intent myIntent = new Intent(Mygame_Menu.this, Mygame.class);
    startActivity(myIntent);
}
Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24