[英]how to show all children in a recyclerview (FirebaseRecyclerOptions) for a specific user
This is how I show data for each user using FirebaseRecyclerOptions.这就是我使用 FirebaseRecyclerOptions 为每个用户显示数据的方式。
But I want to get all children data for only a specific user with a condition by checking users with UID or any other solution.但是我想通过检查具有 UID 或任何其他解决方案的用户来获取具有条件的特定用户的所有子数据。
Ps: This post was edited many times because of the database structure has been changed ps:由于数据库结构发生变化,此贴被多次编辑
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_pdv, container, false);
recyclerView = v.findViewById(R.id.pdv_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
if (user_id.equals("hZBCb9yk8ycamjoVcISjG2y4ZnI2")){
Query query = FirebaseDatabase.getInstance("https://dtechapp-94795-default-rtdb.europe-west1.firebasedatabase.app")
.getReference().child("commande").orderByChild("user_id").equalTo("hZBCb9yk8ycamjoVcISjG2y4ZnI2"); ;
FirebaseRecyclerOptions<Order> options =
new FirebaseRecyclerOptions.Builder<Order>()
.setQuery(query, Order.class)
.build();
pdvAdapter = new PdvAdapter(options);
recyclerView.setAdapter(pdvAdapter);
return v;
}
}
You are creating reference on wrong way.您正在以错误的方式创建参考。
DatabaseReference rootRef =
FirebaseDatabase.getInstance().getReference("pdv");
DatabaseReference pdvRef = rootRef.child("user_id");
By using pdvRef you will get children of user id.通过使用 pdvRef,您将获得用户 ID 的子代。
When you using the following query:当您使用以下查询时:
Query query = FirebaseDatabase.getInstance("https://dtechapp-94795-default-rtdb.europe-west1.firebasedatabase.app")
.getReference("pdv").child("user_id");
It means that you are trying to read data from:这意味着您正在尝试从以下位置读取数据:
rootRef/pdv/user_id
Such a reference actually doesn't exist.这样的引用实际上不存在。 Under your "pdv" node there is no child called "user_id", but one called "fP0LLs83MYZqX9BqYO24IX80F3Q2".
在您的“pdv”节点下,没有名为“user_id”的子节点,而是一个名为“fP0LLs83MYZqX9BqYO24IX80F3Q2”的子节点。 So you either use:
所以你要么使用:
Query query = FirebaseDatabase.getInstance("https://dtechapp-94795-default-rtdb.europe-west1.firebasedatabase.app")
.getReference("pdv").child("fP0LLs83MYZqX9BqYO24IX80F3Q2");
Or if that UID of the user that comes from a Firebase authentication operation, you can then simply use:或者,如果用户的 UID 来自 Firebase 身份验证操作,则您可以简单地使用:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = FirebaseDatabase.getInstance("https://dtechapp-94795-default-rtdb.europe-west1.firebasedatabase.app")
.getReference("pdv").child(uid);
Edit:编辑:
According to one of your comments:根据您的评论之一:
So I want to get data from children of all users at the same time.
所以我想同时从所有用户的孩子那里获取数据。 This is 1st user "fP0LLs83MYZqX9BqYO24IX80F3Q2" I want to get children inside him and at the same time, I want to get children from 2nd user "hZBCb9yk8ycamjoVcISjG2y4ZnI2" and display it in my RecyclerView.
这是第一个用户“fP0LLs83MYZqX9BqYO24IX80F3Q2”,我想让孩子进入他体内,同时,我想从第二个用户“hZBCb9yk8ycamjoVcISjG2y4ZnI2”那里获取孩子,并将其显示在我的 RecyclerView 中。
And according to your database schema , please note that you cannot achieve that.并且根据您的数据库架构,请注意您无法实现。 There is no way you can get all users having such a schema.
您无法让所有用户都拥有这样的架构。 Queries in the Realtime Database work on a flat list of nodes.
实时数据库中的查询在节点的平面列表上工作。 Besides that, you have an unknown key under the UID node, which makes it impossible to query.
除此之外,您在 UID 节点下有一个未知的键,这使得无法查询。 To solve this, you should consider removing one level in your tree, and add "Amine" and "Zouaoui..." as properties inside each UID node.
要解决此问题,您应该考虑删除树中的一层,并在每个 UID 节点内添加“Amine”和“Zouaoui...”作为属性。 Your schema should look like this:
您的架构应如下所示:
Firebase-root
|
--- pvd
|
--- $uid
| |
| --- type: "Amine..."
| |
| --- //Other fields.
|
--- $uid
|
--- type: "Zouaoui..."
|
--- //Other fields.
The code may remain unchanged.代码可能保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.