Android可扩展列表搜索视图

时间:2017-07-12 06:45:50

标签: android expandablelistview expandablelistadapter android-filter

对子值进行排序。 当我键入搜索工作正常,但当我删除键入的字母时,列表不更新。

我的模特课

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;
        }

问题出在哪里?我想我的原始列表在某处被修改了? 有什么帮助吗?

3 个答案:

答案 0 :(得分:1)

在performFiltering方法中进行以下更改:

替换

final ArrayList<Category> processData = originalData;

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

目前您的processData正在引用originalData数组,当您修改processData数组时,您的originalData也会更改。

答案 1 :(得分: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;
        }

我希望它对你有帮助..!

答案 2 :(得分: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");
     }
}}