Home > OS >  how to show all children in a recyclerview (FirebaseRecyclerOptions) for a specific user
how to show all children in a recyclerview (FirebaseRecyclerOptions) for a specific user

Time:10-26

new database structure

This is how I show data for each user using 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.

Ps : This post was edited many times because of the database structure has been changed

    @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;
       }
}

CodePudding user response:

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.

CodePudding user response:

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". 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:

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.

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. To solve this, you should consider removing one level in your tree, and add "Amine" and "Zouaoui..." as properties inside each UID node. Your schema should look like this:

Firebase-root
  |
  --- pvd
       |
       --- $uid
       |     |
       |     --- type: "Amine..."
       |     |
       |     --- //Other fields.
       |
       --- $uid
             |
             --- type: "Zouaoui..."
             |
             --- //Other fields.

The code may remain unchanged.

  • Related