Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract all the user's username who are online from firebase database.

I want to extract all the user's username (highlighted blue) who are online (highlighted red) from a firebase database to a list view.

enter image description here

like image 316
Karun Shrestha Avatar asked Dec 24 '22 02:12

Karun Shrestha


2 Answers

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) { }
});
like image 100
Omar Aflak Avatar answered Dec 26 '22 15:12

Omar Aflak


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

like image 21
Karun Shrestha Avatar answered Dec 26 '22 15:12

Karun Shrestha