-1

I have a collection called 'Quiz' and its document contains Quiz category. How do I get all the category documents? Like only(Science,Technology..etc..)

View Image

I have tried with this code:

 db.collection("Quiz")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d("KKKK : ", document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d("KKKK : ", "Error getting documents: ", task.getException());
                    }
                }
            });

But it never returns a value I have changed Firebase rule to allow read, write: if true;

Flying Dutchman
  • 365
  • 6
  • 13

1 Answers1

1

If db object is defined as follows:

FirebaseFirestore db = FirebaseFirestore.getInstance();

To get all documents that exist within Questions subcollection, please use the following reference:

db.collection("Quiz").document("Science")
    .collection("Questions")
    .get()
    .addOnCompleteListener(/* ... */);

See, you need to add all collection and document names in your reference, not only one, as it is in your actual code right now.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi... But I need List out the first Category Like (Science,Technology...etc...) – Flying Dutchman Apr 01 '20 at 13:24
  • In that case, use the code that you already have and try to [add the results to a list](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview). Then according to what the user selects, add the selected category to your reference as in the reference from my answer. – Alex Mamo Apr 01 '20 at 13:40