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,501
  • 1
  • 22
  • 31
0

You can't compare Strings like that:

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

You have to use the equals() method:

 if(cat.equals("home")){...}
Ahmad
  • 69,608
  • 17
  • 111
  • 137
0

change like this

if(cat.equals("home"))
    {
        counter=0;
    }........
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
0

When comparing strings try this

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