2014-03-04 34 views
-1

我正在使用json。我已經成功地解析了json,並且可以在listview(圖像和文本)中顯示結果。現在我想編寫一個搜索功能來搜索我的json輸出。我已經寫了一個函數,它搜索名稱,但json中的所有名稱都以UpperCase開頭,這就是問題。我還測試了我的代碼沒有UpperCase的情況下,它在那個時候工作。 這是我的代碼Android搜索UpperCase

public void SearchFunction(String searchWord) { 

    itemList.clear(); 
    adapter.notifyDataSetChanged(); 



    if (searchWord.equals(" ")) { 
     for (int i = 0; i < contents.size(); i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(KEY_name, contents.get(i).name); 
      map.put(KEY_id, contents.get(i).id); 
      itemList.add(map); 
     } 
    } else { 
     for (int i = 0; i < contents.size(); i++) { 
      if (contents.get(i).name.indexOf(searchWord) >= 0) { 
       HashMap<String, String> map = new HashMap<String, String>(); 

       map.put(KEY_name, contents.get(i).name); 
       map.put(KEY_id, contents.get(i).id); 

       itemList.add(map); 

      } 

     } 
    } 
    adapter.notifyDataSetChanged(); 

} 




listResult.addTextChangedListener(new TextWatcher() { 
       public void afterTextChanged(Editable s) { 
       } 

       public void beforeTextChanged(CharSequence s, int start, 
         int count, int after) { 

       } 

       public void onTextChanged(CharSequence s, int start, 
         int before, int count) { 

        String searchWord = listResult.getText().toString(); 
        list.setVisibility(View.VISIBLE); 
        SearchFunction(searchWord); 

       } 

      }); 

我要檢查,如果該解決方案是大寫的。我希望我的代碼能夠在有或沒有UpperCase的情況下完美地工作。 請幫我解決。

回答

1

您可以使用.toUpperCase()或.toLowerCase()

我的意思是這樣

contents.get(i).name.toLowerCase().indexOf(searchWord.toLowerCase()) 
+0

謝謝,但只有第一個字母大寫的名字。例如你好 – user3345767

+0

,爲什麼不使用這個變種? – Artem

+0

我試過這個變體,但不工作:( – user3345767