I have need to access a specific child from node, i have only "available key" of the specific child not complete path.
Asked
Active
Viewed 3,671 times
2
-
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 Answers
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
-
Thanks bro for suggestion, "dataSnapshot.getValue().toString()" is totally empty – MIrza Shafique Apr 23 '19 at 16:01
-
Yeah, there we a few mistakes in there. Since this is a query, you need a loop over the snapshot. – Frank van Puffelen Apr 23 '19 at 16:30
-
Thanks bro, i can do this by using loops, but i am searching a way to do this by without loops. – MIrza Shafique Apr 23 '19 at 17:47
-
There isn't. Since you don't know the parent key, you need to run a query. And query results can (on an API level) always contain multiple children, so you need to loop over them. – Frank van Puffelen Apr 23 '19 at 19:48
-
Also note that you never mentioned this goal in your original question. Consider giving a better example of what you're trying to accomplish, what you've already tried, and why that doesn't suit your needs. – Frank van Puffelen Apr 23 '19 at 19:49
-
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
-
-
-
-
first just add the hard code and set both ids and try is it working – Sandeep Malik Apr 23 '19 at 11:52
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) {
}
});

George Gigauri
- 55
- 1
- 9
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