2011-04-25 65 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