2011-09-30 89 views
0

我有一個列表視圖,並在上方顯示一個編輯文本。在編輯文本中,數據從列表視圖中搜索並顯示在編輯文本中。在列表視圖中,一些數據是兩個單詞。例如,列表視圖包含「汽車」,「紅色汽車」,「藍色汽車」。現在,如果我在編輯文本中鍵入c,則只顯示汽車,而不顯示其他兩個。如何在c鍵入的情況下搜索上述三個。這裏是我的代碼...從編輯文本中的列表視圖中過濾文本

@Override 
     public void onTextChanged(CharSequence s, int start, int before,  int count) { 
      wordlength = ed.getText().length(); 
      editsort.clear(); 
      for(int i=0;i<word.length;i++){ 
       if(wordlength<=word[i].length()){ 
        if(ed.getText().toString().equalsIgnoreCase((String) word[i].subSequence(0, wordlength))){ 
        editsort.add(word[i]); 

        } 
       } 
      } 

謝謝...

回答

1

如果word是要搜索的字符串數組:

 public void onTextChanged(CharSequence s, int start, int before, int count) { 
      String searchFor = ed.getText().toString(); 
      editsort.clear(); 
      for(String whole : word){ 
       for(String part : whole.split(" ")) { 
        if(part.startsWith(searchFor)); 
        editsort.add(whole); 
       } 
      } 
     } 
+0

感謝您的答覆。我試過像你說的。現在它顯示包含該字母的所有字符串。例如,如果我現在鍵入c,它將過濾所有包含c的數據。然後,如果我鍵入ca它會顯示所有包含ca的字符串。但在我的情況下,如果ca是type ca,它應該顯示前兩個字母爲car的數據和包含ca在「blue car」之後的數據。它不應該顯示這些字母是否會出現在單詞的中間。希望我已經解釋清楚。 – rose

+0

我已編輯以適合您的解釋。 – kaspermoerch

+0

我正在嘗試做同樣的事情。你能告訴我如何根據edittext中輸入的字母來搜索列表嗎?什麼是editsort這裏? – Namratha

相關問題