[英]retrieve user name from firebase from know uid
I'm quite new to Android and Java, and I'm trying to teach myself doing lockdown.我对 Android 和 Java 很陌生,我正在尝试自学锁定。 Right now I'm trying to retrieve a user's name based on a known
uid
and can't quite figure out the syntax.现在我正在尝试根据已知的
uid
检索用户名,但无法完全弄清楚语法。
My firebase database looks like this我的 firebase 数据库看起来像这样
├── chat
│ ├── -M5D96yP5Ac688-ZPU0C << chatId 1
│ │ ├── info
│ │ │ ├── groupName: Breakfast Club
│ │ │ ├── id: -M5D96yP5Ac688-ZPU0C
│ │ │ └── Users:
│ │ │ ├── 8J7PX3ezjMTuOiAPO1BbOGJGP1g1: true
│ │ │ └── QvmGG2vPfTrdjZBfWP2ZotajYE3: true
│ │ └──Messages
│ │ .....
├── user
│ ├── 8J7PX3ezjMTuOiAPO1BbOGJGP1g1 << userId
│ │ ├── chat:
│ │ │ ├── -M5D96yP5Ac688-ZPU0C: true << chatId 1
│ │ │ ├── -M5DQuUsTJwO6tdVUstC: true
│ │ │ └── -M5DQuUsTJwO6tdVUstC: true
│ │ ├── email: JohnBender@bkfastclub.com
│ │ ├── name: John Bender
│ │ └──notificationKey: "28cb76b1-e2cd-4fa6-bf37-38336dafc45a"
│ ├── kLdxJGA7Yyfch1TthnnAPrnyny93
I have the groupId I'm in, and the userIds of the members stored我有我所在的 groupId,并且存储了成员的 userId
private void getGroupMembers() {
String key = mChatObject.getChatId();
ArrayList<UserObject> mMembers = mChatObject.getUserObjectArrayList();
DatabaseReference chatInfoDb = FirebaseDatabase.getInstance().getReference().child("chat").child(key).child("info").child("users");
DatabaseReference mUserDB = FirebaseDatabase.getInstance().getReference().child("user");
// Query query = mUserDB.orderByChild("uid").equalTo(chatInfoDb.getUid());
}
I just can't quite figure out how to search through the user path and grab the names of the users and store them.我只是不太清楚如何搜索用户路径并获取用户的名称并存储它们。 Ultimately I want to display them in a
RecyclerView
.最终我想在
RecyclerView
中显示它们。
Any help would be much appreciated.任何帮助将非常感激。
If you are trying to get the names of the users under the chat/chatID/info/users
then look at my answer如果您试图获取
chat/chatID/info/users
下的用户名,请查看我的回答
Okay if your key
is actually correct, first loop through all children under chat/chatID/info/users
then grab the names by calling the User
node.好的,如果您的
key
实际上是正确的,首先遍历chat/chatID/info/users
下的所有子项,然后通过调用User
节点来获取名称。
like this:像这样:
//chat ID
String key = mChatObject.getChatId();
//database reference (be careful for lower case or upper case names)
DatabaseReference chatInfoDb = FirebaseDatabase.getInstance().getReference().child("chat").child(key).child("info").child("Users");
DatabaseReference mUserDB = FirebaseDatabase.getInstance().getReference().child("user");
//a first call to the `chat` node:
ValueEventListener chatListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//loop through children
for(DataSnapshot ds: dataSnapshot.getChildren()){
//this will run n times, where n is the number of children under chat/chatID/info/Users
String userId = ds.getKey();
//now we grab the name for every userID under chat/chatID/info/Users
mUserDB.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//grab the name and add it to some list
String name = dataSnapshot.child("name").getValue(String.class);
list.add(name)
}
@Override
public void onCancelled(DatabaseError databaseError) {
// log error
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
}//loop end
}
@Override
public void onCancelled(DatabaseError databaseError) {
//log error
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
chatInfoDb.addValueEventListener(chatListener);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.