2012-11-27 435 views
4

我有一個活動,其頂部帶有一個搜索視圖。 此外,我正在使用自定義列表視圖,並希望從搜索視圖中輸入的關鍵詞過濾應與列表視圖項目中的特定文本視圖相關聯。Android SearchView篩選器列表視圖

enter image description here

+0

執行textWatcher和過濾器適配器。什麼問題? – Yahor10

+0

@ Yahor10好吧,我以前沒有遇到過它。你能指點我一個教程嗎? –

+1

http://stackoverflow.com/questions/5780289/filtering-listview-with-custom-object-adapter and http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ – Yahor10

回答

2
在onQueryTextChange(字符串文本)監聽器使用方法

: adapter.getFilter()過濾器(Text.toString());並在你的BaseAdapter類中實現你的過濾器。 這裏是示例代碼:

@Override 
     public Filter getFilter() 
     { 
      return filter; 
     } 

    private GameFilter filter; 

    private class GameFilter extends Filter 
    { 
     public GameFilter() { } 
      @Override 
        protected FilterResults performFiltering(CharSequence constraint) { 
         FilterResults oReturn = new FilterResults(); 
         ArrayList<ItemDetails> results = new ArrayList<ItemDetails>(); 
        if (orig == null) 
         orig = itemDetailsrrayList;      
         if (constraint != null) 
        { 
         if (orig != null && orig.size() > 0) { 
          for (ItemDetails g : orig) { 
            if (g.getName().toLowerCase().contains(constraint.toString().toLowerCase())) 
           results.add(g); 
         } 
        } 
         oReturn.values = results; 
        } 
        return oReturn; 
        } 

       @SuppressWarnings("unchecked") 
       @Override 
       protected void publishResults(CharSequence constraint, FilterResults results) 
       { 
        itemDetailsrrayList = (ArrayList<ItemDetails>)results.values; 
        notifyDataSetChanged(); 
       } 
      }