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

后端 未结 4 1691
花落未央
花落未央 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:34

    Use Iterator to iterate through dataSnapshot

      public void onDataChange(DataSnapshot snapshot) {
                Iterator items = snapshot.getChildren().iterator();
                int counter = 0;
                while (items.hasNext()) {
                    DataSnapshot item = items.next();
                    Log.i("Result", "email" + item.child("email").getValue().toString());
                    Log.i("Result", "User ID" + item.child("userId").getValue().toString());
                    Log.i("Result", "User Name" + item.child("username").getValue().toString());
                }
            }
    

    Iterator will iterate through all child elements of users and with items.next() you can access child of users's child

提交回复
热议问题