2011-04-25 68 views
1

我已經實現了帶有搜索過濾器的列表視圖,但現在我必須將其更改爲帶有子搜索過濾器的可擴展列表視圖。將會有一個編輯文本作爲搜索欄並過濾所有組的所有孩子。這是場景,帶子搜索過濾器的自定義可擴展列表視圖

*個人 - 對象包括姓名,地址,電話號碼和照片。

第1組 - 好友(兒童1 - 人,兒童2 - 人,兒童3 - 人)

組2 - 家庭(兒童1 - 人,兒童2 - 人)

第3族 - 辦公室(兒童1人,兒童2人,兒童3人,兒童4人)

截至目前,我必須從陣列適配器端口基地可擴展列表適配器和可過濾。有人可以幫我弄這個嗎?謝謝。

回答

7
edit = (EditText)findViewById(R.id.editText1); 
edit.addTextChangedListener(filterTextWatcher); 

private TextWatcher filterTextWatcher = new TextWatcher() { 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

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

    } 

    public void afterTextChanged(Editable s) { 
     ((Filterable) ((ListAdapter) Adapter)).getFilter().filter(edit.getText().toString()); 
    } 
}; 

public class ListAdapter extends BaseExpandableListAdapter implements Filterable { 
    public void notifyDataSetInvalidated() { 
     super.notifyDataSetInvalidated(); 
    } 

    public Filter getFilter() { 
     if (filter == null) 
      filter = new MangaNameFilter(); 
      return filter; 
     } 

private class MangaNameFilter extends Filter { 
    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     // NOTE: this function is *always* called from a background thread, and 
     // not the UI thread. 
     constraint = edit.getText().toString().toLowerCase(); 
     FilterResults result = new FilterResults(); 
     if(constraint != null && constraint.toString().length() > 0) { 
      detailsList = detailsSer.GetAlldetails(); 
      dupCatList = detailsList; 

      ArrayList<detailsEntity> filt = new ArrayList<detailsEntity>(); 
      ArrayList<detailsEntity> lItems = new ArrayList<detailsEntity>(); 
      synchronized(this) { 
       lItems.addAll(dupCatList); 
      } 

      for(int i = 0, l = lItems.size(); i < l; i++) { 
       detailsEntity m = lItems.get(i); 

       if (m.description.toLowerCase().contains(constraint)) 
        filt.add(m); 
       } 

       result.count = filt.size(); 
       result.values = filt; 
      } else { 
       detailsList = detailsSer.GetAlldetails(); 
       dupCatList = detailsList; 
       synchronized(this) { 
        result.count = dupCatList.size(); 
        result.values = dupCatList; 
       } 
      } 
      return result; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults result) { 
      // NOTE: this function is *always* called from the UI thread. 

      filtered = (ArrayList<detailsEntity>)result.values; 

      ArrayList<Integer> IdList = new ArrayList<Integer>(); 
      IdList.clear(); 
      for(int i = 0; i < filtered.size(); i++) { 
       IdList.add(filtered.get(i).catID); 
      } 

      HashSet<Integer> hashSet = new HashSet<Integer>(IdList); 
      midList = new ArrayList<Integer>(hashSet) ; 
      Collections.sort(midList); 
      Adapter = new CategoryListAdapter(context, R.layout.list1, R.layout.list2, filtered, midList); 
      List.setAdapter(Adapter); 
     } 
    }     
} 
+0

你能解釋一點我怎麼能實現這個解決方案。漢克斯 – Zia 2016-05-08 13:16:02

相關問題