2

I have need to access a specific child from node, i have only "available key" of the specific child not complete path.

enter image description here

ADM
  • 20,406
  • 11
  • 52
  • 83
  • What have you tried so far? – Alex Mamo Apr 23 '19 at 10:57
  • Please take a look at: https://firebase.google.com/docs/database/admin/retrieve-data – Joshua Best Apr 23 '19 at 11:01
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in the overflow menu (⠇) of [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Apr 23 '19 at 13:19

4 Answers4

3

Since you know the key of the child you're looking for, you can query on that:

DatabaseReference schoolsRef = FirebaseDatabase.getInstance().getReference().child("School");
Query query = schoolsRef.orderByKey().equalTo("MZ3bW5kLJAQorgnZbYiTaOoWWSG2");

db.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            String email = userSnapshot.child("email").getValue(String.class);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

Note that you can't use orderByChild("id") as explained in my answer here: Firebase Query Double Nested.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

try this :-

DatabaseReference mUserNameReference = FirebaseDatabase.getInstance().getReference("School").child("id").child("id");
    mUserNameReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    String email = dataSnapshot.child("email").getValue().toString());
                   //get all the key value like this
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17
0

Just add this code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();

db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String email = dataSnapshot.child("Email").getValue().toString();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
0

If you have key for that child but not complete path first get key of parent node in list like below I did.

 Arraylist<String> keyList =new Arraylist();
      DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();

        db.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                String key = childSnapshot.getKey();

                String value=  childSnapshot.getValue(String.class);

                keyList.add(key);

            }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

now use for loop or foreach loop to check if that node contains available child key.

for(String key:keyList){



 DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(key).orderByChild("id").equal(availableKey);

            db.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                    String key = childSnapshot.getKey();

                    Object object=  childSnapshot.getValue(Object.class);



                }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });







}

hope this will help you..

Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47