0

對子值進行排序。 當我鍵入搜索工作正常,但當我刪除鍵入的信件列表不更新。Android擴展列表搜索視圖

我的模型類

public class Category { 

    public class SubCategory 
    { 
     public String name; 

     public SubCategory(String name) 
     { 
      this.name = name; 

     } 
    } 

    public String name; 

    public ArrayList<SubCategory> categories; 

    public Category(String name, ArrayList<SubCategory> categories) 
    { 
     this.name = name; 
     this.categories = categories; 
    } 

} 

適配器

private ArrayList<Category> data; 
    private ArrayList<Category> originalData; 


    public CategoryListAdapter(Context context, ArrayList<Category> data) 
    { 
     this.context = context; 
     this.data = data; 
     this.originalData = new ArrayList<>(); 
     this.originalData.addAll(data);  } 


    public Filter getFilter() { 
     MyFilter filter = new MyFilter(); 
     return filter; 
    } 


    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 

      String filterString = constraint.toString().toLowerCase(); 
      Log.i("Tag" , filterString); 
      final ArrayList<Category> processData = originalData; 

      if(filterString.isEmpty()){ 

       results.values = processData; 
       results.count = processData.size(); 
       Log.i("Tag" , "original"+ originalData.get(0).categories.size()); 

      }else { 
       int count = processData.size(); 
       ArrayList<Category> nlist = new ArrayList<>(); 

       for (int i = 0; i < count; i++) { 
        Category cat = processData.get(i); 
        final ArrayList<RequestCategory> req_catgories = processData.get(i).categories; 
        final ArrayList<RequestCategory> new_list = new ArrayList<>(); 

        for (RequestCategory req_catgory : req_catgories) { 
         if (req_catgory.display_name.toLowerCase().contains(filterString)) { 
          new_list.add(req_catgory); 
          Log.i("Sub category", "added"); 
         } 
        } 
        cat.categories = new_list; 
        nlist.add(cat); 
       } 

       results.values = nlist; 
       results.count = nlist.size(); 
      } 
      Log.i("VALUES", results.values.toString()); 
      return results; 
     } 

在那裏有問題的片段?我認爲我的原始列表在某處被修改了? 請幫忙嗎?

+0

當您將文本傳遞給適配器進行過濾時,您是否在那裏應用了任何條件?可能是它沒有通過文本,如果它是空的 –

+0

看到這個鏈接http://www.androidbegin.com/tutorial/android-search-listview-using-filter/,你的原始列表越來越修改,因爲你正在創建參考 – Meenal

回答

0

請添加試試這個做一些改變..!

private ArrayList<Category> data; 
     private ArrayList<Category> searchData; 
     private ArrayList<Category> originalData; 


     public CategoryListAdapter(Context context, ArrayList<Category> data) { 
      this.context = context; 
      this.data = data; 
      this.searchData = data; 
      this.originalData = data; 
     } 


     public Filter getFilter() { 
      MyFilter filter = new MyFilter(); 
      return filter; 
     } 


     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 

      String filterString = constraint.toString().toLowerCase(); 
      Log.i("Tag", filterString); 
      final ArrayList<Category> processData = searchData; 

      if (filterString.isEmpty()) { 

       results.values = processData; 
       results.count = processData.size(); 
       Log.i("Tag", "original" + originalData.get(0).categories.size()); 

      } else { 
       int count = processData.size(); 
       ArrayList<Category> nlist = new ArrayList<>(); 

       for (int i = 0; i < count; i++) { 
        Category cat = processData.get(i); 
        final ArrayList<RequestCategory> req_catgories = processData.get(i).categories; 
        final ArrayList<RequestCategory> new_list = new ArrayList<>(); 

        for (RequestCategory req_catgory : req_catgories) { 
         if (req_catgory.display_name.toLowerCase().contains(filterString)) { 
          new_list.add(req_catgory); 
          Log.i("Sub category", "added"); 
         } 
        } 
        cat.categories = new_list; 
        nlist.add(cat); 
       } 

       results.values = nlist; 
       results.count = nlist.size(); 
      } 
      Log.i("VALUES", results.values.toString()); 
      return results; 
     } 

我希望這對你有幫助..!

+0

沒有使用originalData? –

1

製作按照您的performFiltering方法改變:

更換

final ArrayList<Category> processData = originalData; 

final ArrayList<Category> processData = new ArrayList<>(); 
processData.addAll(originalData); 

目前你的過程數據被引用到originalData陣列,當你修改過程數據數組,那麼你的originalData是也改變了。

+0

當您將文本傳遞給適配器進行過濾時,我仍然收到錯誤 –

+0

,您是否在那裏應用了任何條件?可能是它沒有通過文本,如果它是空的 –

+0

沒有。 。adapter.getFilter()過濾器(newText); 如果沒有文字輸入,我仍會進入空白狀態。但主類別內的子類別列表已修改。 –

0

將下面的代碼替換爲Filter中代碼的一部分。

if(filterString==null||filterString.equals("")||filterString.length()==0){ 
     new_list.addAll(req_catgories); 
}else{ 
    for (RequestCategory req_catgory : req_catgories) { 
    if (req_catgory.display_name.toLowerCase().contains(filterString)) { 
     new_list.add(req_catgory); 
     Log.i("Sub category", "added"); 
    } 
}}