Akhilesh Sharma
Akhilesh Sharma

Reputation: 19

Reading value of dynamic children in firebase android

I am trying to read values from child nodes in firebase database. The thing is that these child nodes are to be added at run time. So, I don't know the name of child as of now.

Is it possible to read common values from these child nodes? the structure is as attached as a image. Blue are dynamic child nodes, and red one is the value to be read.

Database Structure

Upvotes: 0

Views: 875

Answers (1)

Tal Barda
Tal Barda

Reputation: 4287

If I understood your question correctly, blue children are being added dynamically, and you would like to get the names of them at the runtime. Please try the following code:

 DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference().child("data")
                    .child("idsToTrack").child(data_unique_id);

            databaseReference.addChildEventListener(new ChildEventListener() {

                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                            String childName=dataSnapshot.child("name").getValue(String.class);

                        }

                        @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) {

                        }
                    });

This listener gives you a lot of information about the children traffic under a wanted node. At this example, you get the name of the child that was added, at the moment it was added.

Upvotes: 1

Related Questions