英文:
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 ) );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论