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();