peace
peace

Reputation: 71

Android RecyclerView Multiple Filter

I need to use multiple filtering in my project. For example, I have a TASKS page. I want to filter on this page. Priority, responsibles, start date, end date etc. I need to do multiple filtering with criteria such as. What is the correct method? For example, between January 10 and January 12, I would like to list tasks with low priority and medium priority.

Upvotes: 1

Views: 1744

Answers (1)

Amr Jyniat
Amr Jyniat

Reputation: 385

I faced the same problem since a period, and I solved it, you can use this function to add multiple filters.

 public List<Tasks> multipleFilter(String PriorityFilter,String startDateFilter,String endDateFilter,List<Tasks> listAllTasks)
{
    List<Tasks> listTasksAfterFiltering = new ArrayList<>();
    for(Tasks task_obj : listAllTasks)
    {
        String PriorityTask = task_obj.getPriority();
        String startDateTask = task_obj.getStartDate();
        String endDateTask = task_obj.getEndDate();

        if(PriorityFilter.equals(PriorityTask) || PriorityFilter.isEmpty())
            if(startDateFilter.equals(startDateTask) || startDateFilter.isEmpty())
                if(endDateFilter.equals(endDateTask) || endDateFilter.isEmpty())
                    if(!PriorityFilter.isEmpty() || !startDateFilter.isEmpty() || !endDateFilter.isEmpty()){
                        listTasksAfterFiltering.add(task_obj);
                    }
    }

    return listTasksAfterFiltering;

}

First of all, you must get all the items from your list and retrieve the attributes that you want to make a filter on it, then check each field if null or not and if not null compare it with the same attribute for each item, finally check if was the insert value in any field.

Good luck!

Upvotes: 1

Related Questions