简体   繁体   English

如何在特定用户的回收视图(FirebaseRecyclerOptions)中显示所有子项

[英]how to show all children in a recyclerview (FirebaseRecyclerOptions) for a specific user

new database structure新的数据库结构

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.

相关问题 如何将recyclerview中的所有数据传递给另一个活动并在listview或recyclerview中显示它 Firebase - How can I pass all the data in the recyclerview to another activity and show it in the listview or recyclerview Firebase 如果 firestore,用户的信息不会显示在 recyclerview 中获取集合中的特定值 - information of the user doesn't show in recyclerview in getting a specific value in a collection if firestore 如何使用 FirebaseRecyclerOptions 检索 uid 数据 - How to retrieve uid data using FirebaseRecyclerOptions 如何使用 FirebaseUI 库(Java Android)在 recyclerview 中显示特定用户的帖子 - How to show Posts by Specific users in a recyclerview using FirebaseUI library (Java Android) 如何在 recyclerview 上显示 slider? - How can i show the slider on recyclerview? 无法解析符号“FirebaseRecyclerOptions&lt;&gt;” - Can't resolve Symbol 'FirebaseRecyclerOptions<>' 如何使用 React Native 和 Firebase 向不同类型的用户显示特定屏幕 - How can I show specific screens to different type of user with React Native & Firebase 无法解析符号“FirebaseRecyclerOptions” - Cannot resolve symbol 'FirebaseRecyclerOptions' 对于我的 android 应用程序,如何查看或过滤特定用户 ID 的 Firebase 事件以及如何获取触发事件的所有用户 - For my android app , How to watch or filter Firebase event for specific user ID and How to get all all users who have triggered a event Firebase函数如何一次调用访问collections的所有子项? - Firebase functions how to access all children collections by one call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM