Nikant
Nikant

Reputation: 89

How to get User id from Firebase in android?

I want to retrieve the circled part from my firebase database

I have Done this:

 adapter.getRef(currentPosition).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
           adapterKey =  dataSnapshot.getChildren().toString();
           Log.i("adapterKey",adapterKey);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I want to retrieve the Id's generated by firebase in order to access the inner data to EndUsers node. How can I do that?

Upvotes: 0

Views: 1754

Answers (2)

Leonce BALI
Leonce BALI

Reputation: 3

DatabaseReference mDatabaseReference; //you must initialize it 

mDatabaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            Log.i(TAG, "the key is : " + child.getKey());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Upvotes: 1

Benjith Mathew
Benjith Mathew

Reputation: 1221

Try like this

for(DataSnapshot snapshot:dataSnapshot.getChildren()){
//snapshot.getKey()
//snapshot.getValue()
}

Upvotes: 2

Related Questions