I want to extract all the user's username (highlighted blue) who are online (highlighted red) from a firebase database to a list view.
Just tested this, and it works!
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("drivers");
Query query = reference.orderByChild("online").equalTo("true");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, dataSnapshot.toString());
}
public void onCancelled(DatabaseError databaseError) { }
});
I found the answer to the question, thanks to A. Omar and Frank van Puffelen for the help:
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://<your-app>.firebaseio.com/drivers/");
ref.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot driverSnapshot: dataSnapshot.getChildren())
{
if(driverSnapshot.child("online").getValue().toString()=="true"){
Log.d(TAG, driverSnapshot.getKey() + " - " + driverSnapshot.child("online").getValue().toString());
}
}
}
public void onCancelled(FirebaseError firebaseError) {
}
});
this will print only the online users like:
2332424 - true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With