两个字符串比较并不在机器人工作 [英] comparison of two Strings doesn't work in android
问题描述
这是我的code,Eclipse不显示任何错误,程序工作正常,但它根本没有做什么,我想:)
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);
}
应用程序应该两幅图像之间进行选择弹出屏幕上,这取决于存储在XML文件中的标签。 因此,如果标签写着危险,则应该显示的关注 - 图像。 如果标签说:not_dangerous,应该是its_ok - 图像。
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.
如果我打印出来的标签,一个字符串,它的工作原理,它打印正确的危险或not_dangerous,根据不同的标签。
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.
请人帮助!=)
推荐答案
使用 string1.equalsIgnoreCase(东西)
或 .equals(某事);
使用 ==
(字符串)在Java中你是比较它们的相同的参考。像你这样的测试,如果他们两个都是字符串对象。
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.
这篇关于两个字符串比较并不在机器人工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!