0

I have a large list of posts in a social app. I am using FirebaseRecyclerAdapter that is fetching result directly from firebase and listens to any dataa changes made on firebase realtime database. I dont need all the posts, and when i put some condition on the posts, it shows the relevant posts correctly but those posts that are not fulfilling the condition the view shows at its place. Like its show empty list item if condition is not filled and if condition is fulfilled it show list item with data. I want to remove listitems from list that doesnt fulfill the condition. Below is my code. I have a string array that contains the id of business user is following. Post iteam contains a business id, now if post's business id matches with the id of business array post should be shown other wise not.

Query dbQuery = FirebaseDatabase.getInstance().getReference().child("new").child("BusinessPosts").orderByChild("timeStamp");
    FirebaseRecyclerOptions<PostMC> options = new FirebaseRecyclerOptions.Builder<PostMC>()
            .setQuery(dbQuery, PostMC.class).build();

    firebasePostAdapter = new FirebaseRecyclerAdapter<PostMC, PostsViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final PostsViewHolder holder, final int position, @NonNull final PostMC postMC) {
            if (followingBusinessesList.contains(postMC.getBusinessID())) {
                if (source.equals("BPF") || (source.equals("BHF"))) {
                    sharedByDBRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            if (dataSnapshot.child(mAuth.getCurrentUser().getUid()).hasChild(postMC.getPostID())) {
                                holder.shares.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_post_share_colored, 0, 0, 0);
                            } else {
                                holder.shares.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_post_share, 0, 0, 0);
                            }
                        }

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

                        }
                    });
                } else {
                    likedByDBRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            if (dataSnapshot.child(mAuth.getCurrentUser().getUid()).hasChild(postMC.getPostID())) {
                                holder.likes.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_liked, 0, 0, 0);
                            } else {
                                holder.likes.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_like, 0, 0, 0);
                            }
                        }

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

                        }
                    });
                }
                holder.likes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mProcessLike = true;
                        final int postLikes = Integer.parseInt(String.valueOf(postMC.getPostLikes()));
                        likedByDBRef.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                if (mProcessLike) {
                                    if (dataSnapshot.child(mAuth.getCurrentUser().getUid()).hasChild(postMC.getPostID())) {
                                        businessPostsRef.child(postMC.getPostID()).child("postLikes").setValue("" + (postLikes - 1));
                                        likedByDBRef.child(mAuth.getCurrentUser().getUid()).child(postMC.getPostID()).removeValue();
                                        likedToDBRef.child(postMC.getPostID()).child(mAuth.getCurrentUser().getUid()).removeValue();
                                        holder.likes.setText(postMC.getPostLikes());
                                        mProcessLike = false;

                                    } else {
                                        businessPostsRef.child(postMC.getPostID()).child("postLikes").setValue("" + (postLikes + 1));
                                        likedByDBRef.child(mAuth.getCurrentUser().getUid()).child(postMC.getPostID()).setValue("Liked");
                                        likedToDBRef.child(postMC.getPostID()).child(mAuth.getCurrentUser().getUid()).setValue("Like");
                                        holder.likes.setText(postMC.getPostLikes());
                                        mProcessLike = false;

                                    }
                                }
                            }

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

                            }
                        });
                    }
                });


                holder.userImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mAuth.getCurrentUser().getUid().equals(postMC.getBusinessID())) {
                            Intent intent = new Intent(getActivity(), ViewBusinessProfileActivity.class);
                            intent.putExtra("businessID", postMC.getBusinessID());
                            startActivity(intent);
                        }

                    }
                });

                holder.profileLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mAuth.getCurrentUser().getUid().equals(postMC.getBusinessID())) {
                            Intent intent = new Intent(getActivity(), ViewBusinessProfileActivity.class);
                            intent.putExtra("businessID", postMC.getBusinessID());
                            startActivity(intent);
                        }
                    }
                });

                Calendar calendar = Calendar.getInstance(Locale.getDefault());
                calendar.setTimeInMillis(Long.parseLong(postMC.getPostTime()));
                PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
                String ago = prettyTime.format(calendar);
                holder.sub.setText(postMC.getPostMessage());
                holder.time.setText(ago);
                holder.shares.setText(postMC.getPostShares());
                holder.likes.setText(postMC.getPostLikes());
                holder.main.setText(postMC.getBusinessName());
                Glide.with(getActivity()).load(postMC.getBusinessImageUrl()).into(holder.userImage);
                if (postMC.getPostImage().equals("")) {
                    holder.postImage.setVisibility(View.GONE);
                } else {
                    Glide.with(getActivity()).load(postMC.getPostImage()).into(holder.postImage);
                }
            } else {

            }
        }

        @NonNull
        @Override
        public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_single_post, viewGroup, false);
            PostsViewHolder viewHolder = new PostsViewHolder(view);
            return viewHolder;
        }
    };


    userPostsRV.setAdapter(firebasePostAdapter);
    firebasePostAdapter.startListening();

Businesss Data Posts Data

Issue Image List Ite

recyclerViewImage

  • In simple works: I am using firebase recyclerview to populate my data and retrieving those data from firebase realtime database. I don't want to retrieve all of my data but with a condition that is if "Available == yes " then show me the data in recyclerview. My condition is working but the problem is I have 4 data set on firebase realtime database. From them in 3 set value of available is yes .So, I supposed to get 3 list of data but I am getting all 4 set. 3 set data has actual value and 1 set taking the dummy one. How can I stop that? – Adnan Qurban Jun 05 '20 at 04:04
  • I think I understand what the problem is. Before I can answer the question, please add your database structure as a JSON file or at least a screenshot and show us what is the expected result. – Alex Mamo Jun 05 '20 at 09:25
  • sure just 2 minutes i will share the structure of firebase database i am using – Adnan Qurban Jun 05 '20 at 09:30
  • Please check business and posts images, each business has unique id and each post has a business id – Adnan Qurban Jun 05 '20 at 09:41
  • What is the expected result? – Alex Mamo Jun 05 '20 at 09:46
  • expected result is the required posts, but i am getting all the posts – Adnan Qurban Jun 05 '20 at 09:58
  • lets say logged in user is following business X and Y out of Business X,Y and Z, so he must be shown only posts of business X and Y. So when i use the above code i am getting all posts, X and Y's post are fine with the required data, but posts of Z are also showing without actual data. – Adnan Qurban Jun 05 '20 at 10:01
  • Can you reproduce that in a "SQL" language? Something like, I'm looking for all posts that exist within a node where the businessesId is equal to something. – Alex Mamo Jun 05 '20 at 10:06
  • Did you try setting holder's visibility as GONE in the else part? – edison16029 Jun 05 '20 at 10:07
  • select a.* from BusinessPosts a,BusinessData b where a.business_id = b.business_id; Yes i tried, but if i do visibility GONE, its create vacant Spaces in the recyclerView. – Adnan Qurban Jun 05 '20 at 10:12
  • Can you post the XML File of the firebasePostAdapter layout? – edison16029 Jun 05 '20 at 10:13

1 Answers1

0

You can hide the entire view in the else part by setting the visibility to "GONE". In your else part add the following.

if(followingBusinessesList.contains(postMC.getBusinessID())) {
 ...
}
else{
  holder.itemView.setVisibility(View.GONE);
  holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
}
edison16029
  • 346
  • 1
  • 9
  • i have already done the above suggestions. I am not able to post the xml of POST list item as its too long. Can you share email i can email you the details. – Adnan Qurban Jun 05 '20 at 10:30
  • check the newly attached image with name recyclerViewImage, the gap shown above the post due to: holder.itemView.setVisibility(View.GONE); And if i dont make view GONE, the list item shows default values – Adnan Qurban Jun 05 '20 at 10:42
  • I am sorry, i missed a line. as explained in https://stackoverflow.com/questions/41223413/how-to-hide-an-item-from-recycler-view-on-a-particular-condition , you have to change the Recycler View params. Check my edited answer. – edison16029 Jun 05 '20 at 10:51
  • holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0)); solved the issue. Thanks a lot. – Adnan Qurban Jun 05 '20 at 11:03
  • Sure, kindly accept the answer. While the answer i posted is a quick fix to your issue, a more efficient solution would be to add the necessary items to a list and using it to set the adapter. Check https://stackoverflow.com/questions/46156902/make-firebaserecycleradapter-get-data-under-conditions-and-not-all-of-them/46384299#46384299 – edison16029 Jun 05 '20 at 11:06