0

I'm trying to retrieve parent node which is group names like Android11 or The lovers based on the value of child node "userID" if match current logged user.

my database

I initialized arraylist and arrayadapter then looped through datasnapashot using Query. So if order by "userID" match the current user then pass the value of parent group name to arraylist "list_of_group" but I get empty list. What do you think the issue is?

 private void InitializeFields() {
     list_view = (ListView) GroupFragmentView.findViewById(R.id.list_view);
     arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, list_of_groups);
     list_view.setAdapter(arrayAdapter);
 }


 private void RetriveAndDisplayGroups() {
     mAuth = FirebaseAuth.getInstance();
     currentUserID = mAuth.getCurrentUser().getUid();

     groupList = FirebaseDatabase.getInstance().getReference().child("Group Members");

     groupList.orderByChild("userID").equalTo(currentUserID)
             .addValueEventListener(new ValueEventListener() {
                 @Override
                 public void onDataChange(DataSnapshot dataSnapshot)
                 {
                     list_of_groups.clear();
                     if (dataSnapshot.exists()) {
                         for (DataSnapshot ds : dataSnapshot.getChildren())
                         {
                            String grpName = ds.getValue().toString();
                            list_of_groups.add(grpName);
                         }
                         arrayAdapter.notifyDataSetChanged();
                     }

                 }

                 @Override
                 public void onCancelled(@NonNull DatabaseError databaseError) {

                 }

                 ;
             });
 }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Your current data structure makes it easy and easy to find the members for a given group. It does not however make it easy to find the groups for a given user. You'll need to add an additional data structure for that, often known as a reverse index. For more on this, see my answers here https://stackoverflow.com/q/40656589, https://stackoverflow.com/q/41527058, and https://stackoverflow.com/q/27207059. – Frank van Puffelen May 20 '21 at 22:18
  • You should get the value of the node `userID` use `if(userID.equals(currentUserID)){ String grpName = dataSnapshot.getKey(); list_of_groups.add(grpName);}` Obviously, do this inside of `onDataChange()` method, because it is asynchronous. Hope this answers your question. – Adrixb May 21 '21 at 08:23
  • Frank, I will check those links and for sure I will find what I'm looking for. Thanks! – Ali Alhadab May 21 '21 at 15:12

0 Answers0