Home > Back-end >  How to use Where and Orderby to firestore in android
How to use Where and Orderby to firestore in android

Time:04-20

I want to filter Fruits items with its order, but these are diffrent field. How can use the whereEqualto and orderBy?

is there any idea to do this?

mStore.collection("All-ar").whereEqualTo("type","Fruits").orderBy("order").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()){
            for (DocumentSnapshot doc:task.getResult().getDocuments()){
                Items items = doc.toObject(Items.class);
                mItemsList.add(items);
                itemsRecyclerAdapter.notifyDataSetChanged();
            }
        }
    }
});

enter image description here

CodePudding user response:

I see that you are not setting the adapter and also the itemsRecyclerAdapter.notifyDataSetChanged(); is inside the loop. Try this code:

mStore.collection("All-ar").whereEqualTo("type","Fruits").orderBy("order").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful()){
                            for (DocumentSnapshot doc:task.getResult().getDocuments()){
                                Items items = doc.toObject(Items.class);
                                mItemsList.add(items);
                            }
                        }
                        recyclerView.setAdapter(itemsRecyclerAdapter);
                        itemsRecyclerAdapter.notifyDataSetChanged();
                    }
                });

Maybe that is why you don't see the fruits ordered. (Answered according to what I understood).

CodePudding user response:

Are you asking how to filter on two fields?

That should be something like:

mStore.collection("All-ar")
  .whereEqualTo("type","Fruits")
  .whereEqualTo("order", 152)
  .get()
  ...
  • Related