0

I want to apply multiple query filters on a Firestore collection but I have no idea how to do it. If I apply one filter, it works fine.

Part of my code:

private ArrayList<TaskItem> taskList;
private CollectionReference tasks;

taskList = new ArrayList<>();
tasks = firestore.collection("Tasks");

queryData();

private void queryData(){
        taskList.clear();

        tasks.whereEqualTo("recipient", user.getEmail())
                .whereNotEqualTo("status", "Completed")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        for(QueryDocumentSnapshot document : queryDocumentSnapshots){
                            TaskItem item = document.toObject(TaskItem.class);
                            taskList.add(item);
                        }

                        if(taskList.size() == 0){
                            initalizeData();
                            queryData();
                        }

                        adapter.notifyDataSetChanged();
                    }
                });
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Frigyes Vass
  • 139
  • 1
  • 4
  • 17
  • please refer to https://stackoverflow.com/q/26700924/9185361 You can put multiple clauses as tasks.whereEqualTo("recipient", user.getEmail()) .whereNotEqualTo("status", "Completed") .whereEqualTo("someKey", "SomeValue") .get() – Asfar Ali May 26 '21 at 07:57

1 Answers1

1

When you want to filter documents from Firestore using whereEqualTo() together with whereNotEqualTo(), an index is required. To create such an index, please see my answer from the following post:

Once you create the index, you'll be able to map the documents from Firestore into objects of type "TaskItem" and add them to the adapter.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193