-1

So, I would like to load on my activity the Documents' names inside one of my collections (not the attributes).

This is to then be able to click them and load the attributes inside them.

My initial question is: how can I get the ID?

I tried this but didn't work

db.collection("Kit List").document().get().addOnSuccessListener { snapshot ->

        snapshot.id

        firstKitList.add(snapshot.toString()).toString()

       mainListViewAdapter.notifyDataSetChanged()
    }

Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • https://stackoverflow.com/questions/46995080/how-do-i-get-the-document-id-for-a-firestore-document-using-kotlin-data-classes – Gowthaman M Feb 13 '18 at 14:14
  • https://stackoverflow.com/questions/47437291/get-data-ordered-by-document-id-descending-in-firestore?rq=1 – Gowthaman M Feb 13 '18 at 14:14
  • I think the first one is a bit too overkill for what I need. Can't seem to extract what I really need. Would you mind with some extra help? Thanks – Kotlinboiya Feb 13 '18 at 14:22
  • Please add your database structure and tell us what is the exact data that you want to get. – Alex Mamo Feb 13 '18 at 14:56

1 Answers1

0

Assuming that your database looks something like this:

Firestore-root
     |
     --- Kit List (collection)
           |
           --- docId1 (document)
           |
           --- docId2 (document)
           |
           --- //

To get the documents key, please use the following code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference kitListRef = rootRef.collection("Kit List");
kitListRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                String key = document.getId();
                Log.d("TAG", key);
            }
        } else {
            Log.d("TAG", "Error getting documents: ", task.getException());
        }
    }
});

The output will be:

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