Grégori Machado Grégori Machado - 13 days ago 5
Android Question

Search for a specific value in all children in a list in Firebase

I have the following database on Firebase:
firebase database

I want to return a search on users if there is any with the true type.

mDatabaseReference.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("type")) {//I need the verification here.
//action
} else {
//action
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});


Thank you in advance for your attention

Answer

What you are looking for is a filtered query like this:

Query filterQuery = databaseReference.orderByChild("type").equalTo(true);

You can also have a look at this answer for more examples about filtering data and have a look at these docs.

Do let me know if this points you in right direction.

EDIT:

You can check for null by putting this condition in a if statement in onDataChange

dataSnapshot.getValue() != null
Comments