Android if if语句在onItemClickListener中不起作用

时间:2022-11-22 21:28:49

I have a list view that displays values (names) I get from my website, then loads the url link corresponding to that name in a web view using onItemClickListener, that works fine! but when I add a if statement it doesn't read it, can someone tell me what I'm doing wrong?

我有一个列表视图,显示我从我的网站获得的值(名称),然后使用onItemClickListener在Web视图中加载与该名称对应的url链接,这很好!但是当我添加一个if语句时它不会读它,有人能告诉我我做错了什么吗?

Here is the code

这是代码

private AdapterView.OnItemClickListener onPeriodicosListClick = new  AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        cambio="";
        pUrl="";
        noteId = arrayOfWebData.get(position);
        cambio  = noteId.Medio.toString();
        PeriodicosListLayout.setVisibility(View.GONE);
        webLayout.setVisibility(View.VISIBLE);      
        pUrl = noteId.website.toString();
        wvp.loadUrl(pUrl);

        if ( cambio == "Periodico"){
            Toast.makeText(getApplicationContext(),
                    cambio,
                    Toast.LENGTH_LONG).show();
        }
        if ( cambio == "Tv"){
            Toast.makeText(getApplicationContext(),
                    cambio,
                    Toast.LENGTH_LONG).show();
        }

    }
};

the (cambio = noteId.Medio.toString();) can only be Periodico, Radio, Tv (as I have set the value of it to be any of those 3 word only) so all I want is a tost to be display telling what it is!

(cambio = noteId.Medio.toString();)只能是Periodico,Radio,Tv(因为我已经将它的值设置为这3个字中的任何一个)所以我想要的只是显示告诉这是什么!

4 个解决方案

#1


3  

It should not be

它不应该

if(cambio == "Periodico")

but

if(cambio.equals("Periodico"))

#2


2  

Not sure, but if cambio is a String object, the if statement should be

不确定,但如果cambio是一个String对象,那么if语句应该是

if(cambio.equals("Periodico") {
...

#3


1  

The == operator checks to see if two objects refer to the same space in memory (not the content of the string). Strings are objects which means the == operator does not do a content check but a reference check. Use the instance compareTo(String anotherString) or equals(Object) method to compare the contents of strings i.e:

==运算符检查两个对象是否引用内存中的相同空间(而不是字符串的内容)。字符串是对象,这意味着==运算符不进行内容检查,而是进行引用检查。使用实例compareTo(String anotherString)或equals(Object)方法来比较字符串的内容,即:

if (cambio.compareTo("Periodico") == 0) { /* match */ }
if (cambio.compareTo("Tv") == 0){ /* match */ }

There is also an compareToIgnoreCase(String string) and equalsIgnoreCase(Object) variant if case does not matter.

如果情况无关紧要,还有compareToIgnoreCase(String string)和equalsIgnoreCase(Object)变体。

#4


0  

Did you try using a switch statement instead of if? Also what do you get if you output cambio to the logCat using

您是否尝试使用switch语句而不是if?如果你使用cambio输出cambio,你会得到什么?

Log.d(TAG, "cambio: " + cambio);

One last thing. What are you using to connect the List to your activity? A simpleAdapter, baseAdapter, ..?

最后一件事。你用什么来将List连接到你的活动?一个simpleAdapter,baseAdapter,..?

#1


3  

It should not be

它不应该

if(cambio == "Periodico")

but

if(cambio.equals("Periodico"))

#2


2  

Not sure, but if cambio is a String object, the if statement should be

不确定,但如果cambio是一个String对象,那么if语句应该是

if(cambio.equals("Periodico") {
...

#3


1  

The == operator checks to see if two objects refer to the same space in memory (not the content of the string). Strings are objects which means the == operator does not do a content check but a reference check. Use the instance compareTo(String anotherString) or equals(Object) method to compare the contents of strings i.e:

==运算符检查两个对象是否引用内存中的相同空间(而不是字符串的内容)。字符串是对象,这意味着==运算符不进行内容检查,而是进行引用检查。使用实例compareTo(String anotherString)或equals(Object)方法来比较字符串的内容,即:

if (cambio.compareTo("Periodico") == 0) { /* match */ }
if (cambio.compareTo("Tv") == 0){ /* match */ }

There is also an compareToIgnoreCase(String string) and equalsIgnoreCase(Object) variant if case does not matter.

如果情况无关紧要,还有compareToIgnoreCase(String string)和equalsIgnoreCase(Object)变体。

#4


0  

Did you try using a switch statement instead of if? Also what do you get if you output cambio to the logCat using

您是否尝试使用switch语句而不是if?如果你使用cambio输出cambio,你会得到什么?

Log.d(TAG, "cambio: " + cambio);

One last thing. What are you using to connect the List to your activity? A simpleAdapter, baseAdapter, ..?

最后一件事。你用什么来将List连接到你的活动?一个simpleAdapter,baseAdapter,..?