or How to remove specific items from recyclerview?
I would like to display items that has a value(numlikes) of 10 or higher in the database.
Let me know what to learn..
https://i.stack.imgur.com/Qre4R.png
public class TrendingFragment extends Fragment {
private View mRootVIew;
private FirebaseRecyclerAdapter<Post, TrendingFragment.PostHolder> mPostAdapter;
private RecyclerView mPostRecyclerView;
final ArrayList<Integer> arrayList = new ArrayList<>();
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootVIew = inflater.inflate(R.layout.fragment_trending, container, false);
// numLikes value
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("posts");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrayList.clear();
for (DataSnapshot ds: dataSnapshot.getChildren()) {
int numLike = ds.child("numLikes").getValue(Integer.class);
arrayList.add(numLike);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
init(); //display posts
return mRootVIew;
}
init();
private void init() {
mPostRecyclerView = (RecyclerView) mRootVIew.findViewById(R.id.recyclerview_post2);
mPostRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
setupAdapter();
mPostRecyclerView.setAdapter(mPostAdapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mPostRecyclerView.setLayoutManager(mLayoutManager);
}
setupAdapter
private void setupAdapter() {
mPostAdapter = new FirebaseRecyclerAdapter<Post, TrendingFragment.PostHolder>(
Post.class,
R.layout.row_post,
TrendingFragment.PostHolder.class,
FirebaseUtils.getPostRef()
)
{
@Override
public void populateViewHolder(final TrendingFragment.PostHolder viewHolder, final Post model, int position) {
viewHolder.setNumCOmments(String.valueOf(model.getNumComments()));
viewHolder.setNumLikes(String.valueOf(model.getNumLikes()));
viewHolder.setTIme(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
viewHolder.setUsername(model.getUser().getUser());
viewHolder.setPostText(model.getPostText());
Glide.with(getActivity())
.load(model.getUser().getPhotoUrl())
.into(viewHolder.postOwnerDisplayImageView);
if (model.getPostImageUrl() != null) {
viewHolder.postDisplayImageVIew.setVisibility(View.VISIBLE);
StorageReference storageReference = FirebaseStorage.getInstance()
.getReference(model.getPostImageUrl());
Glide.with(getActivity())
.using(new FirebaseImageLoader())
.load(storageReference)
.into(viewHolder.postDisplayImageVIew);
} else {
viewHolder.postDisplayImageVIew.setImageBitmap(null);
viewHolder.postDisplayImageVIew.setVisibility(View.GONE);
}
viewHolder.postLikeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLikeClick(model.getPostId());
}
});
viewHolder.postCommentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), PostActivity.class);
intent.putExtra(Constants.EXTRA_POST, model);
startActivity(intent);
}
});
}
};
}
PostHolder
public static class PostHolder extends RecyclerView.ViewHolder {
ImageView postOwnerDisplayImageView;
TextView postOwnerUsernameTextView;
TextView postTimeCreatedTextView;
ImageView postDisplayImageVIew;
TextView postTextTextView;
LinearLayout postLikeLayout;
LinearLayout postCommentLayout;
TextView postNumLikesTextView;
TextView postNumCommentsTextView;
public PostHolder(View itemView) {
super(itemView);
postOwnerDisplayImageView = (ImageView) itemView.findViewById(R.id.iv_post_owner_display);
postOwnerUsernameTextView = (TextView) itemView.findViewById(R.id.tv_post_username);
postTimeCreatedTextView = (TextView) itemView.findViewById(R.id.tv_time);
postDisplayImageVIew = (ImageView) itemView.findViewById(R.id.iv_post_display);
postLikeLayout = (LinearLayout) itemView.findViewById(R.id.like_layout);
postCommentLayout = (LinearLayout) itemView.findViewById(R.id.comment_layout);
postNumLikesTextView = (TextView) itemView.findViewById(R.id.tv_likes);
postNumCommentsTextView = (TextView) itemView.findViewById(R.id.tv_comments);
postTextTextView = (TextView) itemView.findViewById(R.id.tv_post_text);
}
public void setUsername(String username) {
postOwnerUsernameTextView.setText(username);
}
public void setTIme(CharSequence time) {
postTimeCreatedTextView.setText(time);
}
public void setNumLikes(String numLikes) {
postNumLikesTextView.setText(numLikes);
}
public void setNumCOmments(String numComments) {
postNumCommentsTextView.setText(numComments);
}
public void setPostText(String text) {
postTextTextView.setText(text);
}
}
I've tried many things but nothing worked. The whole posts were either visible or invisible. What should I learn? I have no idea.
Comments
Post a Comment