Firebase for Android, How can I loop through a child (for each child = x do y)

后端 未结 4 1693
花落未央
花落未央 2020-12-24 02:45

This is what my test looks like:

I won\'t use the fields above, it\'s just a dummy. But I would like to go through all the children on \"users\" and for eac

4条回答
  •  轮回少年
    2020-12-24 03:28

    Let say you have a reference to the node users, you can iterate through the nodes as follows:

    reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot child : dataSnapshot.getChildren() ){
                    // Do magic here
                }
            }
    
            @Override
            public void onCancelled(FirebaseError firebaseError) {}
        });
    

    Note that the DataSnapshot child inside the for loop will have the UIDs as key, not users.

提交回复
热议问题