Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparison of two Strings doesn't work in android [duplicate]

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!!=)

like image 285
Silvan Avatar asked Jul 25 '11 16:07

Silvan


People also ask

How can I compare two strings in Android?

In order to compare two strings, we have to use a method called “equals”. Type the following into the parentheses of your If Statement: car1. equals() . In the parentheses of THIS code, write car2 as a parameter.

Can you use == to compare two strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Can we compare two strings in if condition?

== will do an object comparison between the strings in this situation, and although the value may be the same of the String objects, the objects are not the same. Hence why we use String. equals(string); to compare the value of two string objects. So if(u.

When comparing strings Why is == not a good idea?

Using the “==” operator for comparing text values is one of the most common mistakes Java beginners make. This is incorrect because “==” only checks the referential equality of two Strings, meaning if they reference the same object or not.


2 Answers

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.

like image 189
Nikola Despotoski Avatar answered Oct 11 '22 14:10

Nikola Despotoski


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);
like image 21
Archit Avatar answered Oct 11 '22 13:10

Archit