4

I want to filter some ArrayList of datas with search,

In my Activity's onCreate:

        arrayList = getListItemData();
        filteredArrayList = new ArrayList<>();
        filteredArrayList.addAll(arrayList);

        adapter = new NameAdapter(filteredArrayList);
        itemList.setAdapter(adapter);

        searchBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

                adapter.getFilter().filter(s.toString());

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

my adapter with filterable:

public class NameAdapter extends RecyclerView.Adapter<NameAdapter.ViewHolder> implements Filterable {
        private ArrayList<Name> arrayList;
        private CustomFilter filter;

        public NameAdapter(ArrayList<Branch> items) {
            arrayList = items;
            filter = new CustomFilter(NameAdapter.this);
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_name, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.data = arrayList.get(position);


        }

        @Override
        public int getItemCount() {
            return branchArrayList.size();
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder {
            public final View view;
            public final TextView branch;
            public Name data;

            public ViewHolder(View view) {
                super(view);
                this.view = view;
                branch = view.findViewById(R.id.textView_name);

            }

        }

        public class CustomFilter extends Filter {
            private NameAdapter adapter;

            private CustomFilter(NameAdapter adapter) {
                super();
                this.adapter = adapter;
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                filteredArrayList.clear();
                final FilterResults results = new FilterResults();
                if (constraint.length() == 0) {
                    filteredArrayList.addAll(arrayList);
                } else {
                    final String filterPattern = constraint.toString().toLowerCase().trim();
                    for (final Name name : arrayList) {
                        if (name.getName().toLowerCase().startsWith(filterPattern)) {
                            filteredBranchArrayList.add(name);
                        }
                    }
                }
                results.values = filteredArrayList;
                results.count = filteredArrayList.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                this.adapter.notifyDataSetChanged();
            }
        }
    }

filter doesn't work for some reason it clears the recyclerview when I type something

Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
  • https://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview – Steve Ruben Mar 15 '18 at 07:38
  • @SteveRuben I want to see my own mistake – Ege Kuzubasioglu Mar 15 '18 at 07:41
  • 1
    Inside your `Adapter`, `filteredBranchArrayList` and `branchArrayList` point to the same `ArrayList`, so when you `clear()` `filteredBranchArrayList` first thing in `performFiltering()`, it clears the `Adapter`'s `branchArrayList`, as well. I think you're just getting `branchArrayList` in the `Activity`, and `branchArrayList` in the `Adapter` confused. – Mike M. Mar 15 '18 at 08:01

4 Answers4

4

1st make a copy of the branchArrayList in the constructor.like this :-

 private ArrayList<Branch> branchCopy = new ArrayList<>;
 public BranchAdapter(ArrayList<Branch> items) {
        branchArrayList = items;
        branchCopy.addAll(items);
        filter = new CustomFilter(BranchAdapter.this);
    }

your performingFilter

  @Override
  protected FilterResults performFiltering(CharSequence constraint) {
        ArrayList<Branch> branchFilter = new ArrayList<>;
        final FilterResults results = new FilterResults();
            if (constraint.length() == 0) {
                branchFilter.addAll(branchArrayList);
            } else {
                final String filterPattern = constraint.toString().toLowerCase().trim();
                for (final Branch branch : branchCopy) {
                    if (branch.getBranchName().toLowerCase().startsWith(filterPattern)) {
                        branchFilter.add(branch);
                    }
                }
            }
            results.values = branchFilter ;
            results.count = branchFilter.size();
            return results;
        }

Your publishResults

  @Override
  protected void publishResults(CharSequence constraint, FilterResults results) {
      branchArrayList = (ArrayList<Branch>) results.values; // you have done nothing with the filter results 
      notifyDataSetChanged();
  }

Before notifying change the mainList !!

 branchArrayList = (ArrayList<Branch>) results.values;

add this line to publishResults

You have done NOTHING with the filter results

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • no, it will now work: you cannot assign `results.values` to `branchArrayList` - you gotta have two lists - one for all items and the second for filtered ones – pskink Mar 15 '18 at 08:30
  • Sorry @pskink was in a hurry couldnt read his full question , its obvious you need to have two lists !! (beginner logic) – Santanu Sur Mar 15 '18 at 08:43
  • you did nothing with the copy list. What's the use of it ? – Vivek Mishra Aug 21 '21 at 06:03
  • `branchCopy` is the master list (it will store all the objects ) and `branchArrayList` will have the **filtered** list ( it may or may not have all the objects ) @VivekMishra – Santanu Sur Aug 21 '21 at 08:38
0

Try to change your Adapter as

public class BranchAdapter extends RecyclerView.Adapter<BranchAdapter.ViewHolder> implements Filterable {
        private ArrayList<Branch> mArrayList;
        private ArrayList<Branch> mFilteredList;
        private CustomFilter filter;

        public BranchAdapter(ArrayList<Branch> items) {
            mArrayList = items;
            mFilteredList = items;
            filter = new CustomFilter(BranchAdapter.this);
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_branch, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.data = branchArrayList.get(position);
            holder.branch.setText(String.valueOf(mArrayList.get(position).getBranchCode() + " / " + mArrayList.get(position).getBranchName()));

        }

        @Override
        public int getItemCount() {
            return mFilteredList.size();
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder {
            public final View view;
            public final TextView branch;
            public Branch data;

            public ViewHolder(View view) {
                super(view);
                this.view = view;
                branch = view.findViewById(R.id.textView_branch);

            }

        }

        public class CustomFilter extends Filter {
            private BranchAdapter adapter;

            private CustomFilter(BranchAdapter adapter) {
                super();
                this.adapter = adapter;
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                String charString = charSequence.toString();

                if (charString.isEmpty()) {

                    mFilteredList = mArrayList;
                } else {

                    ArrayList<Branch> filteredList = new ArrayList<>();

                    for (Branch branch : mArrayList) {

                        if (branch.getBranchName().toLowerCase().contains(charString)) {

                            filteredList.add(branch);
                        }
                    }

                    mFilteredList = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilteredList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                mFilteredList = (ArrayList<Branch>) filterResults.values;
                notifyDataSetChanged();
              //  this.adapter.notifyDataSetChanged();
            }
        }
    }
matin sayyad
  • 575
  • 3
  • 10
0

update your branchArrayList in the publishResults method before doing notifyDataSetChanged

Steve Ruben
  • 1,336
  • 11
  • 20
0

update your PublishResults() with below code

 @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            filteredBranchArrayList = (ArrayList<Branch>) filterResults.values;
            notifyDataSetChanged();
        }
Omkar
  • 3,040
  • 1
  • 22
  • 42