Reputation: 262
I am grabbing a String from text file on a url and saving it to sharedPreferences ... then the next time it grabs the String, it compares it against the old one stored in shared preference and if its different it notifies the user.
I am getting the string from url fine and its saving in sharedPreferences, but when I try to compare them with an if (x == y) statement it always results as not the same;
Here is the onPostExecute()
protected void onPostExecute(String result) {
String Input;
String oldInput;
Input = result.toString();
oldInput = currentSavedSettings();
if (Input == oldInput){
Toast.makeText(MainActivity.this, "fixed", Toast.LENGTH_LONG).show();
} else {
savePrefs("CURRENT_SETTINGS_FILE", Input);
//the toasts were used to ensure that neither were returning null
Toast.makeText(MainActivity.this, oldInput, Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, Input, Toast.LENGTH_LONG).show();
// the following is commented out until I fix this issue
//createNotification();
}
}
and the currentSavedSettings which is getting the old CURRENT_SETTINGS_FILE
private String currentSavedSettings() {
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String result = sp.getString("CURRENT_SETTINGS_FILE", null);
return result;
}
and for good measure here is what i am using to save the SharedPreference
private void savePrefs(String key, String value) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
Upvotes: 0
Views: 744
Reputation: 5077
Did you try this,
passcode == pref_passcode // is not the same according to Java
String passcode;
pref_passcode.contentEquals(passcode); //try this or
pref_passcode.equals(passcode)
You should always use one of these to compare string. do not use equal signs (==)
Upvotes: 0
Reputation: 133560
Use .equals
or .equalsIgnoreCase
to compare strings.
For more info
How do I compare strings in Java?
Upvotes: 3