2016-04-25 80 views
0

在我的android应用程序中,我测试了一些字符串(我从另一个活动中获取)以查看它们是否为空。如果没有,我建我的网址,从这些值,例如:(编者)多重值测试,如何替换许多其他if语句?

//country only 
    if (!fetchData_country.isEmpty() && fetchData_city.isEmpty() && fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()){ 
     query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + URL_LANGUAGE; 
     Log.i("INFO", "Url country only : " + query_url); 
    } 
    //country & price min 
    else if(!fetchData_country.isEmpty() && fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) { 
     query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE; 
     Log.i("INFO", "Url country & price_min : " + query_url); 
    } 

    //city & price_min 
    else if(fetchData_country.isEmpty() && !fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) { 
     query_url = URL_PRODUCT + "?" + URL_SEARCH_CITY + id_city + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE; 
     Log.i("INFO", "Url city & price_min : " + query_url); 
    } 

    // country & city & pmin 
    else if(!fetchData_country.isEmpty() && !fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) { 
     query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + "&" + URL_SEARCH_CITY + id_city + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE; 
     Log.i("INFO", "Url country & city & pmin : " + query_url); 
    }//many values to test still... 

我不知道我怎样才能提高这些测试的部分,感谢您的帮助!

+0

@Palcenteü可以开发一个小PLS?这不是很清楚,谢谢 – Jay

+0

!fetchData_pmin.isEmpty()&& fetchData_pmin.isEmpty()这根本没有意义 –

+0

@NongthonbamTonthoi,sorry!fetchData_pmin.isEmpty()&& fetchData_pmax.isEmpty(),除此之外,我的代码工作,我正在寻找改进,同时测试thx(代码编辑) – Jay

回答

1

类似这样写的东西

String query_url = URL_PRODUCT + "?" + (!fetchData_country.isEmpty() ? "URL_SEARCH_COUNTRY + id_country " : "") 
      +(!fetchData_country.isEmpty() ? "&":"")+ (!fetchData_pmin.isEmpty() ? " + URL_SEARCH_PRICE_MIN + id_pmin " : "") 
      +(!fetchData_pmin.isEmpty() ? "&":"")+ (!fetchData_city.isEmpty() ? " + URL_SEARCH_CITY + id_city " : ""); 
+0

有一点适应,它工作正常!谢谢@ user5323999,接受的答案 – Jay

+0

顺便说一句,这是什么样的文字风格? – Jay

+0

三元运营商的多个条件 –