0

I want to make a list view but in condition if the current user id exists in the Members then display the group name and that is a key like test in the screenshot

enter image description here

The list view works, but displays all groups and what I need is to display only the groups that the current user is a member of.

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        groupFragmentView = inflater.inflate(R.layout.fragment_groups, container, false);
        GroupRef = FirebaseDatabase.getInstance().getReference().child("Groups");
        mGroupsList = (ListView) groupFragmentView.findViewById(R.id.list_view);

        final ArrayAdapter arrayAdaptor = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,mGroups);
        mGroupsList.setAdapter(arrayAdaptor);


        mGroupsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
            {
                String currentGroupName = adapterView.getItemAtPosition(position).toString();

                Intent groupChatIntent=new Intent(getContext(),GroupChatActivity.class);
                groupChatIntent.putExtra("groupName",currentGroupName);
                startActivity(groupChatIntent);
            }
        });


                GroupRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                String value = dataSnapshot.getKey();
                
                String currentUser= mAuth.getUid();
                
                UserRef = GroupRef.child(value).child("Group Details").orderByChild("Members").equalTo(currentUser);
                
                mGroups.add(value);

                arrayAdaptor.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });
KENdi
  • 7,205
  • 2
  • 14
  • 26
Bremboar
  • 1
  • 1
  • Share the crash log – Manoj Perumarath Feb 28 '19 at 07:27
  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseAuth.getUid()' on a null object reference at com.example.capstpne.educhat.GroupsFragment$2.onChildAdded(GroupsFragment.java:86) – Bremboar Feb 28 '19 at 07:42
  • `FirebaseAuth.getInstance().getCurrentUser().getUid()` use this method to get uid – Manoj Perumarath Feb 28 '19 at 07:46
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Manoj Perumarath Feb 28 '19 at 07:48
  • thank you that solved the current user but still i have no idea on how to set condition to display inly the groups that the current user is a member of – Bremboar Feb 28 '19 at 08:02

0 Answers0