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);
}
}
}
});