Carl
Carl

Reputation: 23

On Android, how to get the child identifier that was generated automatically in Firebase Database?

The following image shows what I mean:

How to get the child identifier that was automatically generated in the Firebase Database (which they are shown in the red rectangles), and bring it to Android Studio?

@Override                    
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    reference = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(**What must be written here?**); }

Upvotes: 0

Views: 27

Answers (1)

Samin Jilani
Samin Jilani

Reputation: 155

You always have to use multiple DatabaseReference objects to approach each nested childs.in your case, it will be something like this to start with.

 DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Usuarios");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
            
                for(DataSnapshot dataSnapshot: snapshot.getChildren()) {
                  
                      Log.d("TAG", dataSnapshot.getkey());
    reference2 =FirebaseDatabase.getInstance().getReference(dataSnapshot.getkey());
///and so on//
                }
            }
    
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
    
            }
        });

Upvotes: 1

Related Questions