0

Always shows "else" statement. I don't know that's wrong here. please help.

The Android code:

public void setResult(final int val,final String cat){
        if(cat=="home")
        {
            counter=0;
        }
        else if(cat=="featured")
        {
            counter=1;
        }
        else if(cat=="editorial")
        {
            counter=2;
        }
        else if(cat=="gallery")
        {
            counter=3;
        }
        else
        {
            Toast.makeText(parentActivity, "Invalid category: " + cat + "\n" + "cat.toString() is: " + cat.toString(), 5).show();
        }
        Toast.makeText(this.parentActivity, "Callback got val: " + val + "\n Category is: " + cat, 5).show();
    }

The HTML code (within script tag):

function onload()
{
    window.MyHandler.setResult(2,"featured");
}

The output here is:

Invalid category: featured
cat.toString() is: featured
Callback got val: 2
Category is: featured
Cookie Monster
  • 107
  • 1
  • 1
  • 6

4 Answers4

0

You need to use equals() method instead ==

For example

cat.equals("featured");
Roman Black
  • 3,471
  • 1
  • 21
  • 31
0

You can't compare Strings like that:

if(cat=="home"){...}

You have to use the equals() method:

 if(cat.equals("home")){...}
Ahmad
  • 64,492
  • 17
  • 108
  • 135
0

change like this

if(cat.equals("home"))
    {
        counter=0;
    }........
NagarjunaReddy
  • 8,591
  • 10
  • 62
  • 96
0

When comparing strings try this

if (cat.equals("home")) {
    counter = 0;
}
Eeshwar
  • 282
  • 3
  • 17