0

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();

}

2 Answers2

0

Whenever comparing Strings use the following approach

string1.equals(string2)

Don't use '==' operator for strings
See this for more detailed answer Java String.equals versus ==

Community
  • 1
  • 1
0
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))
Lior
  • 832
  • 6
  • 6
  • Error PIN Appear Again :s :s – Ysen Eycen Nov 04 '16 at 20:49
  • @YsenEycen it doesnt mean that the comparison was worng. print here the value of the 'userid' value in the sharedPref `Log.d(TAG, userpin + " " userpin1);` – Lior Nov 04 '16 at 20:58
  • Code work only when i open the setting activity and i set the value in the editText , otherwise without opening the setting activity code dosn't work – Ysen Eycen Nov 04 '16 at 21:08
  • Solved : I forget to load PreferenceManager.setDefaultValues(this, R.xml.preferences_xml, false); – Ysen Eycen Nov 04 '16 at 21:17