2013-07-03 61 views
0

嘿傢伙什麼是搜索我的對象列表的最佳方式,他們返回一些字符串,姓氏和名字爲例。Android搜索列表,字符串

在這裏,我目前如何搜索,但我的搜索需要匹配我不想要的整個字符串。搜索需要它匹配部分字符串,如我們手機上的聯繫人列表,並忽略大小寫。

if (searchQ.equalsIgnoreCase(child.first_name)) { 
    addChildToList(child); 
    } 

我試過包含並以例如開始,他們沒有工作。這是怎麼回事?

謝謝!乾杯!

+2

使用textwatcher上的EditText和用getFilter() – user2260168

回答

1

您可以使用TextWatcher在列表中以這種方式進行搜索,作爲一個例子,你定義一個TextWatcher的一個EditText(搜索框):

TextWatcher filterTextWatcher = new TextWatcher() { //TextWatcher to Filter RealTime Input Data 
      public void beforeTextChanged(CharSequence s, int start, int count,int after) 
      { 
      } 

      public void onTextChanged(CharSequence s,int start, int before,int count) //Filter Data When Entering Data On ItemType Dialog TextView 
      { 
      } 

      @Override 
      public void afterTextChanged(Editable arg0) 
      { 
      } 
     }; 

     EditText filterText = (EditText)dialog.findViewById(R.id.edtItemFilter); 
     filterText.addTextChangedListener(filterTextWatcher); 

然後onTextChange()回調函數,您可以發送在適配器

public void onTextChanged(CharSequence s,int start, int before,int count) 
     { 
      ListView lv = getListView(); 
      ListAdapter adapter = (ListAdapter) lv.getAdapter(); 
      lv.setTextFilterEnabled(true); 
      adapter.getFilter().filter(s.toString()); 
     } 
+0

嘿偉大的答案:插入字符作爲參數s到你的適配器與用getFilter或設置列表用過濾光標篩選列表!對不起,我忘了提及我正在使用文本觀察器。我的問題是,我應該如何進行搜索以比較字符串,以便可以通過字符串的幾個字母來找到它們,如包含 – NightSkyCode

+1

,如果我理解得很好,則可以使用字符串(在textwatcher中),並將它發送給您列表適配器(你的列表查詢),並在你的查詢中使用類似%your%的語句來過濾包含你的字符串的選擇。 –

+1

另一種解決方案是從頭開始編寫getFilter方法,並將您在TextWatcher中的函數發送給getFilter並使用期望的數據contains()函數進行檢查,如果您希望的數據包含inputed字符串,則將其添加到ArrayList中(它有點複雜在評論中) –