2016-04-25 51 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

三元運營商的多個條件 –