简体   繁体   English

从实时数据库中获取子项的多个值

[英]Get multiple values from child from Realtime Database

How can I get multiple child values with one Firebase Listener?如何使用一个 Firebase 侦听器获取多个子值? For example I want to get the username and full name and save each of them in a string, how would I go about doing this.例如,我想获取用户名和全名并将它们分别保存在一个字符串中,我将如何执行此操作 go。 I'm guessing with a child event listener?我在猜测一个儿童事件监听器? 结构

Can anyone help me out?谁能帮我吗? Thanks.谢谢。

Try the following:尝试以下操作:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");

reference.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
      String name = dataSnapshot.child("username").getValue(String.class);
      String fullName = dataSnapshot.child("fullname").getValue(String.class);

  }
 @Override
public void onCancelled(DatabaseError databaseError) {
      throw databaseError.toException();
     }
});

First get the current user, then reference the uid of the user, attach a listener and you can retrieve the values.首先获取当前用户,然后引用用户的uid,附加一个监听器,您可以检索值。

This might help这可能有帮助

for (DataSnapshot ALL_USERS: dataSnapshot.getChildren()) {
   String all_users=(ALL_USERS.getValue().toString());
   String name = all_users.child("username").getValue(String.class);
   String fullName = all_users.child("fullname").getValue(String.class);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 firebase 实时数据库中获取不同孩子的值? - How to get values from different child in firebase realtime database? 从实时数据库获取哈希图值 - Get Hashmap Values from realtime database 从 Firebase 实时数据库获取 url 值 - get the url values from Firebase Realtime database 如何从Firebase实时数据库获取所有子值并将其存储在数组列表中 - How to get all child values from firebase realtime database and store it in an array list 从孩子内部的实时 Firebase 获取价值 - Get Value From Realtime Firebase Inside Child 从我的 firebase Realtime-Database 中的所有用户获取过滤的列表值,其中列表是用户的子级 - Getting filtered list values from all users in my firebase Realtime-Database where the list is a child of user 努力从Firebase实时数据库中获取价值 - Struggling to take values from Firebase Realtime Database 从 firebase 实时数据库中单个孩子下的多个推送 ID 中检索数据 - Retrieve data from multiple push ids under a single child in firebase realtime database Firebase 实时数据库 - 如何将来自多个活动的数据存储在同一个子元素下? - Firebase Realtime Database - how to store data from multiple activities under the same child element? 如何从 FIrebase 实时数据库而不是整个列表中获取一个孩子 - How to get just one child from FIrebase Realtime Database instead of whole list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM