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!!=)
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.
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.
== 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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With