如何获取在firebase中唯一id内的数据

时间:2021-05-29 16:45:45
 DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");

There's a unique id for each user inside Users child... inside that is the data o

用户内部的每个用户都有一个唯一的id……里面是数据o

1 个解决方案

#1


2  

You need to iterate inside the unique id to be able to get the data there:

您需要在唯一id内进行迭代,才能获得那里的数据:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
 String name=datas.child("name").getValue().toString();
   //gets data
  }
}
@Override
public void onCancelled(DatabaseError databaseError) {
  }
});

Example if you have:

如果你有例子:

 Users
  uniqueid
     name: peter

The above will iterate inside the unique id and give you the name "peter"

上面的代码将在唯一id中迭代,并给您命名为“peter”

#1


2  

You need to iterate inside the unique id to be able to get the data there:

您需要在唯一id内进行迭代,才能获得那里的数据:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
 String name=datas.child("name").getValue().toString();
   //gets data
  }
}
@Override
public void onCancelled(DatabaseError databaseError) {
  }
});

Example if you have:

如果你有例子:

 Users
  uniqueid
     name: peter

The above will iterate inside the unique id and give you the name "peter"

上面的代码将在唯一id中迭代,并给您命名为“peter”