如果键仅返回true,如何使用join从Firebase数据库检索数据? [英] How to retrieve data from Firebase Database with join if key only return true?
问题描述
我想问一下如何检索只有user2在Android中加入的组?
I want to ask how to retrieve group that only user2 join in Android?
"users": {
"user1": {
"name": "User 1"
},
"user2": {
"name": "User 2"
}
}
"groups": {
"groupA": {
"name": "A Group"
},
"groupB": {
"name": "B Group"
}
}
"userGroup": {
"user1": {
"groupA": true,
"groupB": true
},
"user2": {
"groupB": true
}
}
我已经尝试过
DatabaseReference userGroupRef = rootRef.child("userGroup/user2")
但不能显示组名
I already try
DatabaseReference userGroupRef = rootRef.child("userGroup/user2")
but cannot show the name of the groups
但是如果我使用
DatabaseReference groupsRef = rootRef.child("groups")
user2
未加入的组也显示.
But if I use
DatabaseReference groupsRef = rootRef.child("groups")
the group that user2
not joining also showing.
firebase是否有类似groupsID in userGroup/user2
的查询?
Does firebase has a query like groupsID in userGroup/user2
?
编辑_1
我使用FirebaseRecyclerAdapter
来显示rootRef.child("groups")
中的列表,该列表显示了所有组,即使user2没有加入.
I use FirebaseRecyclerAdapter
to show the list from rootRef.child("groups")
that shows all the groups even if user2 is not joining.
groupsRef = rootRef.child("groups");
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Group, GroupHolder>(Group.class, R.layout.group_item_layout, GroupHolder.class, groupsRef) {
@Override
protected void populateViewHolder(GroupHolder viewHolder, Group group, int position) {
viewHolder.setGroupName(group.getName());
}
};
groupRecyclerView.setAdapter(adapter);
推荐答案
This use-case is specifically addressed by the FirebaseIndexRecyclerAdapter
. You pass in two references/queries: one for the so-called index (the keys with true
values that you want to show) and one for the actual object that you want to load for each matching key.
在您的情况下:
groupsRef = rootRef.child("groups");
indexRef = rootRef.child("userGroup/user2");
FirebaseIndexRecyclerAdapter adapter = new FirebaseIndexRecyclerAdapter <Group, GroupHolder>
(Group.class, R.layout.group_item_layout, GroupHolder.class, indexRef, groupsRef) {
@Override
protected void populateViewHolder(GroupHolder viewHolder, Group group, int position) {
viewHolder.setGroupName(group.getName());
}
};
这篇关于如果键仅返回true,如何使用join从Firebase数据库检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!