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

后端 未结 4 1677
花落未央
花落未央 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.

    0 讨论(0)
  • 2020-12-24 03:34
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference();
    myRef.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot item_snapshot:dataSnapshot.getChildren()) {
    
          Log.d("item id ",item_snapshot.child("item_id").getValue().toString());
          Log.d("item desc",item_snapshot.child("item_desc").getValue().toString());
         }
      }
    }
    
    0 讨论(0)
  • 2020-12-24 03:34

    Use Iterator to iterate through dataSnapshot

      public void onDataChange(DataSnapshot snapshot) {
                Iterator<DataSnapshot> 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

    0 讨论(0)
  • 2020-12-24 03:41

    The easiest way is with a ValueEventListener.

        FirebaseDatabase.getInstance().getReference().child("users")
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            User user = snapshot.getValue(User.class);
                            System.out.println(user.email);
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
    

    The User class can be defined like this:

    class User {
      private String email;
      private String userId;
      private String username;
      // getters and setters...
    }
    
    0 讨论(0)
提交回复
热议问题