How to retrieve the list of data under 1 child in Firebase Database in Android Studio


How to retrieve the list of data under 1 child in Firebase Database in Android Studio



Data stored in firebase:


29-06-2018(date)

-AAAA
-25142(jobno)
-Park station(address)
-BMW(model)

-BBBB
-85142(jobno)
-Base station(address)
-Ford(model)



Here I want all the children under -BBBB. Don't want to loop through AAAA. How to get directly the child of BBBB. I'm having data (date, BBBB). Just want to get jobno, address, model of BBBB. Please suggest me a solution.



My code is here


DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference pwd = database.child("29-06-2018").child("BBBB");

pwd.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String a = ds.child("jobno").getValue(String.class);
String b = ds.child("address").getValue(String.class);
String c = ds.child("model").getValue(String.class);

}
}

@Override
public void onCancelled(DatabaseError error) {

}

});




2 Answers
2



You're listening to a single child /29-06-2018/BBBB. By looping over dataSnapshot.getChildren() you're looping over each property, and then try to reach a child property for each. That won't work, so you should get rid of the loop in onDataChange:


/29-06-2018/BBBB


dataSnapshot.getChildren()


onDataChange


pwd.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot ds) {
String a = ds.child("jobno").getValue(String.class);
String b = ds.child("address").getValue(String.class);
String c = ds.child("model").getValue(String.class);
}





Thanks for the solution and quick response. just a small change in your code not "ds" its "dataSnapshot" .
– mohan
Jun 29 at 14:42






Woops, good catch! Fixed.
– Frank van Puffelen
Jun 29 at 14:50


DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference pwd = database.child("29-06-2018").child("BBBB");

pwd.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

String a = dataSnapshot.child("jobno").getValue(String.class);
String b = dataSnapshot.child("address").getValue(String.class);
String c = dataSnapshot.child("model").getValue(String.class);


}

@Override
public void onCancelled(DatabaseError error) {

}

});






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV