How do i find corresponding user using the userid in reviews in firebase rtdb

Viewed 17

I want to fetch corresponding user data from the path users/ after fetching the reviews to a recyclerview

So I have done the 2nd part that all review data is fetched on the list map and ready to display on RecyclerView.

But before that, I need to fetch Username and Userpicture from path users/ (not all of them but the only ones whose id is present in reviews)

So how do I do this using the Firebase query?

I mean if we have the userid inside the path it will be easy to find it by looking for the key value.

but the problem is I don't have it in users/

the user id is the child key

So the question is how do I search for the child key? instead of searching for a value.

please see the image below for Realtime Database structure:

firebase rtdb

1 Answers

According to your last comment:

I'm displaying the reviews for each project. But if I fetch all data under the review path I will only get the date, review, and the UID. But after getting that UID, I need to search for the UID in the users collection to get the User picture and username of that specific user. To display along with the reviews.

To solve this you need to create a reference that points to a specific project, then get all reviews, and based on the UID that you get, create another database call to get user data. So please try the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference projectIdRef = db.child("reviews").child("projects").child("ds1619951195");
projectIdRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> reviewTask) {
        if (reviewTask.isSuccessful()) {
            for (DataSnapshot reviewSnapshot : reviewTask.getResult().getChildren()) {
                String date = reviewSnapshot.child("name").getValue(String.class);
                String review = reviewSnapshot.child("review").getValue(String.class);
                String user = reviewSnapshot.child("user").getValue(String.class);

                DatabaseReference uidRef = db.child("users").child(user);
                uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DataSnapshot> userTask) {
                        if (userTask.isSuccessful()) {
                            DataSnapshot userSnapshot = userTask.getResult();
                            String username = userSnapshot.child("username").getValue(String.class);
                            String picture = userSnapshot.child("picture").getValue(String.class);
                            Log.d("TAG", username + "/" + picture);
                        } else {
                            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
                        }
                    }
                });

            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});
Related