Search code examples
javaandroidfirebase-realtime-databasefirebaseui

Could I use if (snapShot.hasChild(uid) in firebaseRecycleOptions? I am using Firebase Recycle adapter to retrieve data under node: "Groups"


private void RetrieveAndDisplayGroups() {
    FirebaseRecyclerOptions<GroupModel> options = new FirebaseRecyclerOptions.Builder<GroupModel>()
            .setQuery(FirebaseDatabase.getInstance().getReference().child("Groups"), new SnapshotParser<GroupModel>() {
                @NonNull
                public GroupModel parseSnapshot(@NonNull DataSnapshot snapshot) {
                    // I tried to use if statment before return.
                    return new GroupModel(snapshot.child("groupName").getValue().toString(), snapshot.child("groupDescription").getValue().toString());
                }
            }).build();
    modelAdapter = new GroupModelAdapter(options, getContext());
    recyclerExploreGroupView.setAdapter(modelAdapter);
}

So I am making a group chat app. In my app, I want to show the users only the groups they have created. In the database, under node "Groups", I have all the groups created by all users.

I am using firebase recycle adapter to retrieve data from firebase real time database. I am not familiar with how FirebaseRecycleOptions work. I think it returns a set of the model object I passed.

I try to use if statement before new firebaseRecycleOptions return. But I got red line. The goal is to check if each group has a child as a requirement. And only retrieve the ones who have it. Any help would be appreciated.


Solution

  • If you want to only show a subset of the child nodes in the adapter, you should use a query to retrieve only those child nodes from the database.

    It's hard to be certain without seeing your database, but using this query in the adapter might be an option:

    FirebaseDatabase.getInstance().getReference()
        .child("Groups")
        .orderByChild("groupName")
        .startAt(null)
    

    Since null is the first value in Firebase Realtime Database ordering, this will return any node that has a groupName value.