Android字符串值超出if else语句

huangapple go评论67阅读模式
英文:

Android String value out of if else statement

问题

我正在编写一个简单的应用程序,从一个网站上抓取一些数据。
我能够获取数据并将其放入一个字符串值中,但是对于网站的构造函数来说,该值的类名不同,这取决于该值是负数还是正数。

我的问题是:如果从网站获取的值为null,如何将该值赋值给我的字符串?

我尝试过使用if-else语句,但不能正确地将正确的值传递给我的ParseAdapter。

我的代码:

String percentage_red = element.select(perc_red).text();
String percentage_green = element.select(perc_green).text();

if (percentage_red == null) {
    String percentage = percentage_red;
} else {
    String percentage = percentage_green;
}
parseItems.add(new ParseItem(name, price, percentage));

percentage未导出到我的适配器。

英文:

I'm writing a simple app taht scrape some data from a web site.
I'm able to get data and put this in a String value, but as for the costructor of the web site, the value has different class name depending if the value is negative or positive.

My question is: how to assign the value at my string if one of the value get from web site is null?

I try with an if-else statement but isn't work to pass correct value at my ParseAdapter.

My code:

                    String percentage_red = element.select( perc_red ).text();
                    String percentage_green = element.select( perc_green ).text();

                    if(percentage_red == null){
                        String percentage = percentage_red;
                    }else {
                        String percentage = percentage_green;
                    }
                    parseItems.add( new ParseItem( name,price,percentage ) );

percentage isn't export to my adapter

答案1

得分: 0

Declare percentage variable outside the if..else statement, so you can access that variable outside of if..else :

String percentage;

if(percentage_red == null){
    percentage = percentage_red;
}else {
    percentage = percentage_green;
}
parseItems.add( new ParseItem( name,price,percentage ) );
英文:

Declare percentage variable outside the if..else statement, so you can access that variable outside of if..else :

    String percentage;

    if(percentage_red == null){
        percentage = percentage_red;
    }else {
        percentage = percentage_green;
    }
    parseItems.add( new ParseItem( name,price,percentage ) );

huangapple
  • 本文由 发表于 2020年5月4日 03:07:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定