I am designing an application using android studio and firebase. Please see the structure of my database here:

Here I have a parent Sellers node in that seller id as immediate child. Each seller Id contains the details of the seller like name, Shop Type and token.
Here in figure I have shown two sellers with shop type as CarshowRoom and one is having ShopType as supermarket (Actually there are many sellers and want to loop through all sellers).
I like to retrieve device token of sellers if they have a particular ShopType, for example if the seller have ShopType as car showroom I like to get the value of token.
For that I write the code like
query=sellerRef.child("Sellers").orderByChild("ShopType").equalTo("CarShowRoom");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String sellerToken= snapshot.child("token").getValue().toString();
Toast.makeText(SendEnquiryActivity.this, sellerToken, Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
But my code is not working, any body please help
CodePudding user response:
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
Your onDataChange will need to handle this list, by looping over snapshot.getChildren with something like this:
query=sellerRef.child("Sellers").orderByChild("ShopType").equalTo("CarShowRoom");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) { // 