Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parent node in android firebase [duplicate]

This is my current code. How can I get the parent node of the first name

my Firebase DB

final Query userQuery = mRef.orderByChild("First Name");

    userQuery.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            map.clear();
            for (DataSnapshot child: dataSnapshot.getChildren())
            {
                String key = child.getKey().toString();
                String value = child.getValue().toString();
                map.put(key,value);
            }
like image 387
K.Ermac Avatar asked Feb 28 '26 22:02

K.Ermac


1 Answers

You should call the getKey() method in your retrieved dataSnapshot:

  final Query userQuery = mRef.orderByChild("First Name");

  userQuery.addChildEventListener(new ChildEventListener() {
     @Override
     public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        map.clear();
        //Get the node from the datasnapshot
        String myParentNode = dataSnapshot.getKey();
        for (DataSnapshot child: dataSnapshot.getChildren())
        {
           String key = child.getKey().toString();
           String value = child.getValue().toString();
           map.put(key,value);
        }
like image 158
Francisco Durdin Garcia Avatar answered Mar 02 '26 12:03

Francisco Durdin Garcia