How to search in multiple nodes in Firebase Database?

Please Share On:

In this tutorial, I will show you how to get the value from multiple node and set value in your textview.

Here, we will get the value of total_recycled_count from count node and email and name from users node.

The solution is pretty simple. To get the value from two different nodes you need to query your database twice.


//Initialize variable
        currentUser = mAuth.getCurrentUser();

        if(currentUser != null) {
            final String uid = currentUser.getUid();

            FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
            DatabaseReference databaseReference = firebaseDatabase.getReference().child("count");
            Query query = databaseReference.orderByChild(uid).equalTo(uid);

            ValueEventListener valueEventListener = new ValueEventListener() {
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //test for null and validity
                    if(dataSnapshot.exists() && dataSnapshot.getValue()!= null) {

                        for(DataSnapshot ds : dataSnapshot.getChildren()) {
                            String totalRCount = Objects.requireNonNull(ds.child("total_recycled_count").getValue()).toString();

                            //set totalRecycledCount in a profile textview

                            DatabaseReference uidRef = firebaseDatabase.getReference().child("users").child(uid);
                            ValueEventListener eventListener = new ValueEventListener() {
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    String name = dataSnapshot.child("name").getValue(String.class);
                                    String email = dataSnapshot.child("email").getValue(String.class);
                                    Log.d("TAG", name + " / " + email);

                                    //set profile in result textviews
                                    //Don't forget to initialize your profile_name and profile_email and get their xml reference, which is not shown in this code.


                                public void onCancelled(@NonNull DatabaseError databaseError) {}

                public void onCancelled(@NonNull DatabaseError databaseError) {}


You may interest on the following topics:

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @2023. All Right Reserved.

Social media & sharing icons powered by UltimatelySocial