Ysen Eycen Ysen Eycen - 1 month ago 13
Android Question

Can't Compare EditText Value with SharedPreference Value

When I enter a value in the EditText, i can't to compare it with a Value in My SharedPreference .

This is my EditText Preference

<EditTextPreference

android:defaultValue="1234"
android:inputType="number"
android:key="userid"
android:maxLength="4"
android:singleLine="true"
android:summary="To Access App setting set a PIN"
android:title="Set PIN" />


And This is My Methode code:

private void GoToSettings(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
String userpin = prefs.getString("userid", "");
String userpin1 = txtpin.getText().toString();
if(userpin.equals(userpin1)){
/*Intent intent = new Intent(FingerprintScannerActivity.this, PreferencesActivity.class);
startActivity(intent);*/
Toast.makeText(getApplicationContext(),
"Good",
Toast.LENGTH_SHORT).show();
}else Toast.makeText(getApplicationContext(),
"Error PIN",
Toast.LENGTH_SHORT).show();

}

Answer
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
String userpin = prefs.getString("userid", "");

change it to : String userpin = prefs.getString("userid", null); its more efficient to return null instead of empty string and then you can use:

if((userpin == null) ? (userpin1 == null) : userpin.equalsIgnoreCase(userpin1))
Comments