how to search the groups that the user participates in Firebase?

0

I have an android java app and want to know how do I search the groups that a user participates in ...

How do I join to find only the groups that the user participates in? (the attendee table links the group and user tables)?

    
asked by anonymous 09.08.2018 / 18:13

1 answer

0

If you want to know what groups the Z1FZPBz9m0T1yg21DBfRgI2bGFO2 participates in, you can do:

Query query = db.child("attendee").orderByChild("userUID").equalTo("Z1FZPBz9m0T1yg21DBfRgI2bGFO2");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot datasnapshot) {
        for (snap : datasnapshot) {
            String groupUID = snap.getKey();
            // Utilizar o groupID
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {

    }
});

Update: To access the group object:

Query query = db.child("attendee").orderByChild("userUID").equalTo("Z1FZPBz9m0T1yg21DBfRgI2bGFO2");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot datasnapshot) {
        for (snap : datasnapshot) {
            String groupUID = snap.getKey();
            // Utilizar o groupID para achar o objeto grupo
            db.child("group").child(groupUID).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnap) {
                    Group group = dataSnap.getValue(Group.class)
                    //Adicionar o group à lista
                }

                @Override
                public void onCancelled(DatabaseError error) {

                }
            });
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {

    }
});
    
09.08.2018 / 18:54