gpt4 book ai didi

java - 如何显示按最喜欢的评论过滤的回收者 View 项目?

转载 作者:行者123 更新时间:2023-12-01 19:18:57 26 4
gpt4 key购买 nike

我想创建一个小型社交应用程序。我快完成了,但我不知道如何根据最喜欢的评论项在 RecyclerView 中显示评论项。

这是代码

 //This is commentAdapter
public void onBindViewHolder(@NonNull final Myholder myHolder, final int i) {
final ModelComments modelComments = commentsList.get(i);
String cLikes = commentsList.get(i).getcLikes(); //this is for like

myHolder.user_comment.setText(modelComments.getComment());
myHolder.clickBtn.setText(cLikes);//to show how many likes the user get



//This is commentsActivity
RecyclerView recyclerView;
FirebaseAuth firebaseAuth;
FirebaseUser user;
private CommentAdapter commentAdapter;
private List<ModelComments> commentsList;

recyclerView = findViewById(R.id.comment_recycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//show newest comment first and then old
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager);
commentsList = new ArrayList<>();
commentAdapter= new CommentAdapter(this, commentsList);
recyclerView.setAdapter(commentAdapter);

showAllComment();
}

private void showAllComment() {
try {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("comments");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
commentsList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
ModelComments modelComments = snapshot.getValue(ModelComments.class);
commentsList.add(modelComments);
}

commentAdapter.notifyDataSetChanged();
}

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

}
});
} catch (Exception e) {
e.printStackTrace();
}
}

再说一遍,有什么办法可以实现这一点吗?如果您不明白,请评论此问题。

最佳答案

正如我们所看到的,您已经通过执行以下操作完成了绑定(bind)

commentsList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
ModelComments modelComments = snapshot.getValue(ModelComments.class);
commentsList.add(modelComments);
}
commentAdapter.notifyDataSetChanged();

首先,您清除了列表,然后添加了注释,最后调用了notifyDataSetChanged。您错过的是在通知数据更改之前对列表进行排序

请检查此链接以了解如何对列表进行排序。 Sort ArrayList of custom Objects by property

关于java - 如何显示按最喜欢的评论过滤的回收者 View 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com