expandable-recycler-view icon indicating copy to clipboard operation
expandable-recycler-view copied to clipboard

Is it possible to filter results?

Open hackaprende opened this issue 8 years ago • 4 comments

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

hackaprende avatar Mar 30 '17 22:03 hackaprende

@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.

paul-turner avatar Mar 31 '17 16:03 paul-turner

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

hackaprende avatar Mar 31 '17 17:03 hackaprende

Awesome, thanks for following through with the solution :)

paul-turner avatar Mar 31 '17 18:03 paul-turner

Whats that listToFilter value?

Suganyasrii avatar Jan 22 '18 12:01 Suganyasrii