how do I search the groups the user participates in Firebase?

Asked

Viewed 144 times

0

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

minha estrutura no Firebase

how do Join to search only the groups that the user participates in (the Attendee table links the group and user tables)?

1 answer

0

If you want to know what are the groups where the user Z1FZPBz9m0T1yg21DBfRgI2bGFO2 participates, 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) {

    }
});

Updating: 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) {

    }
});
  • in which case this code would return me the group key, right? What if I need to return the Group object? for each group that the user is part of... because what I need is to add to a list each Group object related to the group that the user participates in

  • I updated my reply to show how to access each group object

  • If in my table "Attendee", in the place of "userUID" I had a list of "userUID". How the query would look to find the groups that user is part of...?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.