Home > database >  How to retrieve uid data using FirebaseRecyclerOptions
How to retrieve uid data using FirebaseRecyclerOptions

Time:03-06

I can't retrieve data from firebase under uid. When I try to retrieve data without using uid it works perfect but when I try to retrieve data from uid it doesn't work, can anyone help me to find out data.

FirebaseRecyclerOptions<Model> options =
            new FirebaseRecyclerOptions.Builder<Model>()
                    .setQuery(FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()), Model.class)
                    .build();

    myAdopter = new MyAdopter(options);
    binding.adminRecycler.setAdapter(myAdopter);

CodePudding user response:

Since you're reading a node at a lower level in the tree, the adapter will be populated with the child nodes of that root - typically the properties of the individual user. These children are not longer valid instances of the Model class.

If you want to show a list of a single user, you can use a query like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid()
Query query = FirebaseDatabase.getInstance().getReference().child("Users").orderByKey().equalTo(uid);
FirebaseRecyclerOptions<Model> options =
            new FirebaseRecyclerOptions.Builder<Model>()
                    .setQuery(query, Model.class)
                    .build();

Now you have query that matches just one child node, and that node is a valid Model object.

  • Related