1

In my onCreate() I have:

if(sharedPref.getString("name",null)==""){EditText.setText("Something");}
else{EditText.setText(sharedPref.getString("name",null));}

And then in my onStop() I have:

sharedPrefEditor.putString("name",EditText.getText().toString());

The EditText shows only the hint when I first install and run it. It does seems to display the correct text when it's started later, however.

Latcie
  • 701
  • 1
  • 7
  • 21

5 Answers5

1

Don't use == to compare content of Strings. Use equals() instead :

if(sharedPref.getString("name",null).equals("")){
          EditText.setText("Something");
}

Why should I use equals instead of ==

Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • Thank you, that was the problem in this particular instance. But now my EditText seems to be giving off a nullPointerException. I initialized it in the activity, and defined it in the onCreate(). – Latcie Jun 16 '13 at 16:50
  • @Latcie Try to find where it occurs. It said in the logcat. – Alexis C. Jun 16 '13 at 16:53
0

First of all you are not using correctly the shared preferences. You are typing null as second parameter which is the value you will get if the key you typed as first parameter is not defined. This is not a problem itself but then in your if sentence you are comparing it to an empty string.

So first of all use equals instead of ==and then if you want to receive an empty string if the key is not defined type an empty string as second parameter of SharedPreferences.gerString method.

Hope it helps ;)

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • Actually, for this case, I needed to compare the string to null. When the app is first installed, there should be no sharedPreference, meaning everything should be null. Am I interpreting this correctly? – Latcie Jun 16 '13 at 17:57
  • you could say that there is an empty sharedpreferences, you can compare it to whatever you want, if there is no such value stored, the result of the _getString_ function will be the second parameter, so it hasn't to be necessary null. – zozelfelfo Jun 16 '13 at 19:49
0

The operator == can not be used to compare strings in Java.

if(string1.equals(string2){
}

Also, you set a value to SharedPreferences, but you never commit the changes.

sharedPrefEditor.commit();

OnCreate

String prefsVal = sharedPref.getString("name", null);

if(prefsVal.equals(null)){ //If default value was returned
    EditText.setText("Something");
}else{
    EditText.setText(prefsVal);
}

OR
The following will turn your posted code into a single line solution. The second parameter passed to SharedPref is the default value to be returned if no vale was found for the given key.

EditText.setText(sharedPref.getString("name", "Something");

OnStop

sharedPrefEditor.putString("name", EditText.getText().toString());
sharedPrefEditoy.commit();
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
0

If "Something" is to be used if preference not set, then:

  EditText.setText(sharedPref.getString("name","Something"));

getString() will automatically return "Something" if preference is not set. No need of extra code.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0
String strName = sharedPref.getString("name","");

if(!strName.isEmpty()){
          EditText.setText(strName);
}else{
          EditText.setText("Something");
}
Bishan
  • 15,211
  • 52
  • 164
  • 258