18

here's my code, Eclipse doesn't show any errors, program's working fine, but it simply doesn't do exactly what i want:)

    View image_view_danger_rate = (ImageView) findViewById(R.id.origin);
    View image_view_origin = (ImageView) findViewById(R.id.danger_rate);

    String entry_tag = (String) descriptionResultView.findViewById(resID).getTag();

    String dangerous = "dangerous";
    String not_dangerous = "not_dangerous";

    if ( entry_tag == dangerous) {
        image_view_danger_rate.setBackgroundResource(R.drawable.attention);
    }else if ( entry_tag == not_dangerous) {
        image_view_danger_rate.setBackgroundResource(R.drawable.its_ok);
        image_view_origin.setBackgroundResource(R.drawable.artificial);
    }

The application should choose between two images to pop-up on the screen, depending on a tag stored in the xml file. So, if the tag says "dangerous", then should be shown the "attention"-image. If the tag says "not_dangerous", there should be the "its_ok"-image.

Now, displaying the images without an if-comparison works perfectly.

If i print out the tags as a string, it works, it prints correctly "dangerous" or "not_dangerous", depending on the tag.

But if there's a if-comparison as shown above, nothing happens, no image is shown.

Please anyone help!!=)

7
  • +1 for an elaborate and complete question. Commented Jul 25, 2011 at 17:01
  • This question has been asked on this site more times than I care to find. Please use the search function. Commented Jul 25, 2011 at 17:29
  • @Woot4Moo Duplication is not necesarilly bad. blog.stackoverflow.com/2010/11/… Commented Jul 25, 2011 at 17:34
  • @Woot4Moo Your link to the supposedly duplicate question does not even have to do with this question too. Commented Jul 25, 2011 at 17:37
  • @Marcelo even still how is this subtly different than any of the other examples on this site about people not knowing how to do string comparison in java? Commented Jul 25, 2011 at 17:37

4 Answers 4

45

Use string1.equalsIgnoreCase("something) or .equals("Something");

With == (for strings) in java you are comparing they are of same reference. Like you did is the test if both of them are strings objects.

Sign up to request clarification or add additional context in comments.

1 Comment

I just learned something very important! =D it worked for me...thank you for this quick answer!
13

In java, a==b is used to compare 2 references, not the objects themselves.

so if you have 2 strings that you want to compare, use the equals() method on String. for eg

boolean resultOfComparison=stringA.equals(stringB);

Comments

5

Use

entry_tag.equals(dangerous)

you're comparing actual String objects, not their content. In Java, operators are not overloaded so == can't be used to compare strings.

Comments

2

In Java, if you want to compare Strings, you need to use equals():

if (entry_tag.equals(dangerous)) {
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.