0

I'm trying to show all the posts in the database except the users I follow & my posts. Here is what I've done so far:

public final List<Post> mDefaultPost = new ArrayList<>();
public void readDefaultPosts()
    {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mDefaultPost.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Post post = snapshot.getValue(Post.class);
                    if (snapshot.child("ingrediant").getValue() != null) {
                        ArrayList<String> mList = new ArrayList();
                        for (DataSnapshot ds : snapshot.child("ingrediant").getChildren()) {
                            mList.add(ds.getKey() + ":" + ds.getValue());
                        }
                        post.setIngrediantsList(mList);
                }
                    post.setVideo((Boolean) snapshot.child("isVideo").getValue());

                    if(!post.getPublisher().equals(firebaseUser.getUid()))
                    {
                        mDefaultPost.add(post);
                    }
                }
                postDefaultAdapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

currently, it shows all posts except the current user's posts which is great, but I want to exclude the posts of the users I follow also. How can I do that?

To make it easier for you to help me, Here is how I show the posts of the users I follow on the home page:

 private void checkFollowing()
    {
        followingList = new ArrayList<>();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child("following");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                followingList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren())
                {
                    followingList.add(snapshot.getKey());
                }
                readPost();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError)
            {
            }
        });
    }
    private void readPost()
    {

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                postList.clear();
                for(DataSnapshot snapshot : dataSnapshot.getChildren())
                {
                    Post post = snapshot.getValue(Post.class);
                    for(String id : followingList)
                    {
                        if(post.getPublisher().equals(id))
                        {
                            if (snapshot.child("ingrediant").getValue() != null){
                                ArrayList<String> mList  = new ArrayList();
                                for (DataSnapshot ds:snapshot.child("ingrediant").getChildren()){
                                    mList.add(ds.getKey()+":"+ds.getValue());
                                }
                                post.setIngrediantsList(mList);
                            }
                            post.setVideo((Boolean) snapshot.child("isVideo").getValue());
                            postList.add(post);
                        }
                    }
                }
                postAdatper.notifyDataSetChanged();
}

I'm struggling to combine the two code snippet to achieve the result I want. please help me

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
SSS
  • 77
  • 2
  • 8
  • If you consider at some point in time to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), here is an [answer](https://stackoverflow.com/questions/46979375/firestore-how-to-structure-a-feed-and-follow-system/52153332#52153332) that might help. – Alex Mamo Sep 21 '21 at 05:51
  • unfortunately, I'm not using it. but thank you for the suggestion – SSS Sep 21 '21 at 12:51

0 Answers0