Android的Firebase,我如何通过一个child(每个子= x do y)循环

时间:2022-02-23 23:58:59

This is what my test looks like:

我的测试是这样的:

Android的Firebase,我如何通过一个child(每个子= x do y)循环

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 each email return a:

我不会用上面的字段,它只是一个假的。但是我想把“用户”上的所有孩子都一一列举出来,每回邮件我都要回复a:

System.out.println(emailString);

The only way I found of listing an object is using firebaseAdapter, is there another way of doing it?

我发现列出一个对象的唯一方法是使用firebaseAdapter,还有其他方法吗?

3 个解决方案

#1


21  

The easiest way is with a ValueEventListener.

最简单的方法是使用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) {
                }
            });

#2


3  

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.

注意,for循环中的DataSnapshot子将拥有uid作为键,而不是用户。

#3


2  

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());
     }
  }
}

#1


21  

The easiest way is with a ValueEventListener.

最简单的方法是使用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) {
                }
            });

#2


3  

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.

注意,for循环中的DataSnapshot子将拥有uid作为键,而不是用户。

#3


2  

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());
     }
  }
}