1

I'm trying to retrieve a document id by calling doc.getId() but I can't call it from this code

        db.collection("Pegawai").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(e != null) {
                    Log.d(TAG, e.toString());
                }
                if (queryDocumentSnapshots != null) {
                    for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            Pegawai pegawai = doc.getDocument().toObject(Pegawai.class);
                            pegawai.setId(doc.getId());
                            pegawaiList.add(pegawai);

                            pegawaiListAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });

I've tried this code, and apparently, I can call doc.getId() with this code, but this code isn't populating my recyclerview at all

        db.collection("Pegawai").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(e != null) {
                    Log.d(TAG, e.toString());
                }
                if (queryDocumentSnapshots != null) {
                    for (DocumentSnapshot doc : queryDocumentSnapshots) {
                        Pegawai pegawai = doc.toObject(Pegawai.class);
                        pegawai.setId(doc.getId());
                        pegawaiList.add(pegawai);
                    }
                }
            }
        });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NightKnight
  • 38
  • 1
  • 1
  • 6

3 Answers3

2

If you are using FirestoreRecyclerAdapter with Recyclerview do this.

 DocumentSnapshot snapshot = options.getSnapshots().getSnapshot(position);
            String dbKey = snapshot.getId();
            Log.d(TAG, "The database Key is : "+ dbKey);

Then you can bind it on view holder like this

    holder.setDocumentId(dbKey);

Then you can you retrieve it anywhere, depending on your getters and setters. I hope this will help someone out there.

Ally Makongo
  • 269
  • 5
  • 14
  • You can also do this, You will get a unique value everytime! String _key = db.collection("requests") .document() .getId(); – Ally Makongo Aug 08 '20 at 10:06
1

A DocumentChange object has a method getDocument() which returns a QueryDocumentSnapshot object. This is a subclass of DocumentSnapshot which means it also has a getId() method. So you should be able to use doc.getDocument().getId() to get the ID of the document being processed.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I get it working by creating a method to load my data and call it whenever a data is changed and it's like a roundabout way. But the `getDocument()` method works like a charm! I didn't think it was that simple – NightKnight Jul 17 '19 at 06:17
-1

The following is how to solve it in Javascript, maybe this will help you to convert it to Java. Using the following method, we can retrieve the document id, dynamically. Notice that we use snapshotChanges(). This is important because once we subscribe to this function, contained in the subscription is the document id.

saveMyValuesHere: any[] = []

getPegawai() {

    return this.db.collection('Pegawai').snapshotChanges();

  }

 getPegawai().subscribe(serverItems => {
      this.saveMyValuesHere = [];
      serverItems.forEach(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        this.saveMyValuesHere.push({ id, ...data});
      });
    });

Zakariah Siyaji
  • 989
  • 8
  • 27