bignerdranch / expandable-recycler-view

[DEPRECATED]
MIT License
1.21k stars 290 forks source link

Is it possible to filter results? #347

Open hackaprende opened 7 years ago

hackaprende commented 7 years ago

Hi, is it possible to filter results with this library? what would be a possible implementation?

paul-turner commented 7 years ago

@Almaral-Engineering This isn't something that is implemented by the library, but can be done outside the adapter just like any RecyclerView Adapter.

Apply your filter to the list of Parent/Child objects, you'll now have a reduced list. Update the list on the adapter, and call the individual notify methods. OR call the less efficient notifyParentDataSetChanged/setParentList methods on the adapter to just tell the adapter to refresh everything.

hackaprende commented 7 years ago

Thanks @paul-turner!, I did it with the setParentList and it works now! For others who see this thread, I did this:

private final class ChildObjectFilter extends Filter {
        @Override
        protected FilterResults performFiltering(final CharSequence constraint) {
            final String filterString = constraint.toString().toLowerCase();
            final FilterResults results = new FilterResults();

            final ArrayList<ParentObject> filteredParentObjects = new ArrayList<>();
            for (ExpandableWrapper<ParentObject, ChildObject> expandableWrapper : listToFilter) {
                final ArrayList<ChildObject> filteredChildObjects = new ArrayList<>();
                final ParentObject ParentObject = expandableWrapper.getParent();

                for (final ChildObject ChildObject : ParentObject.getChildObjectList()) {
                    if (ChildObject.getName().toLowerCase().contains(filterString)) {
                        filteredChildObjects.add(ChildObject);
                    }
                }

                if (filteredChildObjects.size() > 0) {
                    final ParentObject filteredParentObject =
                            new ParentObject(ParentObject.getId(), ParentObject.getDescription(),
                                    ParentObject.getEnabled(), filteredChildObjects);
                    filteredParentObjects.add(filteredParentObject);
                }
            }

            results.values = filteredParentObjects;
            results.count = filteredParentObjects.size();
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(final CharSequence constraint, final FilterResults results) {
            final ArrayList<ParentObject> ParentObjectFilteredList =
                    (ArrayList<ParentObject>) results.values;

            setParentList(ParentObjectFilteredList, true);
        }
    }
paul-turner commented 7 years ago

Awesome, thanks for following through with the solution :)

Suganyasrii commented 6 years ago

Whats that listToFilter value?