user1001635
user1001635

Reputation: 3342

if statement fail to check the condition

I have a if statement that does not work, it doesn't check its condition even if it is correct and I can't understand this behavior. please help me to find the error. this is my code:

public Dialog onCreateDialog(int id) {
      AlertDialog.Builder alert = new AlertDialog.Builder(this);
     final SharedPreferences userPreferences = this.getSharedPreferences("strings", MODE_PRIVATE);
     final Editor edit = userPreferences.edit();

     switch (id) {
     case 1:
            alert.setTitle(lang_menu[3]);//"Gestione Preferiti"
         alert.setIcon(R.drawable.ic_menu_star);
         final String namesFav = userPreferences.getString("Name_Fav", "");
         final String locfav = userPreferences.getString("Loc_Fav", "");
         final String numfav = userPreferences.getString("Num_Fav", "");
         final String subnumfav = userPreferences.getString("SubNumFav", "");

        final String[] namesFavs = namesFav.split(";");
        final String[] locfavs= locfav.split(";");
        final String[] numfavs= numfav.split(";");
        final String[] subnumfavs = subnumfav.split(";");

         alert.setSingleChoiceItems(namesFavs, -1, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int item) {
                pos = item;

            }
        });
        alert.setPositiveButton(lang_btn[2], new DialogInterface.OnClickListener() //"Visualizza"
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
              //if (SharedCode.isOnline(content)){
                 if (pos == -1){
                    Toast.makeText(content.getApplicationContext(), lang_error[2], Toast.LENGTH_LONG).show();//"Nessun preferito selezionato"
                 }else{
                    if (namesFavs[pos] == ""){
                        Toast.makeText(content.getApplicationContext(), lang_error[3], Toast.LENGTH_SHORT).show();//"Pagina inesistente"

                    }else{
                        prova= locfavs[pos];
                        SharedCode.loc = locfavs[pos].;
                        SharedCode.num = Short.parseShort(numfavs[pos]);
                        SharedCode.sub_num= Byte.parseByte(subnumfavs[pos]);
//-----------------------------------------------------------------------------------
                        if (SharedCode.loc == "aertel"){//this is the if that doesn't work
                            IrelandTeletext.getPage(content);
                        }
///////////////////////---------------------------------------------------------------
                    }


                }
                pos = -1;
            }
        });
break;
}
AlertDialog MyAlert  = alert.create();
    return MyAlert;
 }

enter image description here

Upvotes: 1

Views: 571

Answers (2)

Jordy Langen
Jordy Langen

Reputation: 3591

You can't compare Strings like that in Java. You should use String.equals(...). The explanation for this quite simple. The == operator checks if the objects are the same. Those strings are different objects with the same value.

Upvotes: 2

Jave
Jave

Reputation: 31846

To compare Strings you should use the yourString.equals(other) or yourString.equalsIgnoreCase(other) method rather than ==.

Upvotes: 4

Related Questions