Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何显示按最喜欢的评论筛选的recyclerview项目?_Java_Android_Android Recyclerview_Social Networking - Fatal编程技术网

Java 如何显示按最喜欢的评论筛选的recyclerview项目?

Java 如何显示按最喜欢的评论筛选的recyclerview项目?,java,android,android-recyclerview,social-networking,Java,Android,Android Recyclerview,Social Networking,我想创建一个小型社交应用程序。我差不多完成了,但我不知道如何在回收视图中显示注释项,基于最受欢迎的注释项 这是代码 //This is commentAdapter public void onBindViewHolder(@NonNull final Myholder myHolder, final int i) { final ModelComments modelComments = commentsList.get(i); String cLikes = commen

我想创建一个小型社交应用程序。我差不多完成了,但我不知道如何在
回收视图中显示注释项,基于最受欢迎的注释项

这是代码

 //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();
    }
}
//这是commentAdapter
public void onBindViewHolder(@NonNull final Myholder Myholder,final int i){
final ModelComments ModelComments=commentsList.get(i);
String cLikes=commentsList.get(i).getcLikes();//这是给like的
myHolder.user_comment.setText(modelcoments.getComment());
myHolder.clickBtn.setText(cLikes);//显示用户得到多少喜欢
//这是评论活动
回收视图回收视图;
FirebaseAuth FirebaseAuth;
FirebaseUser用户;
私有评论适配器;
私人列表评论列表;
recyclerView=findViewById(R.id.comment\u recycler);
LinearLayoutManager layoutManager=新的LinearLayoutManager(此);
//先显示最新评论,然后显示旧评论
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager);
commentsList=新的ArrayList();
commentAdapter=新的commentAdapter(此,commentsList);
recyclerView.setAdapter(commentAdapter);
showallcoment();
}
私有void showallcoment(){
试一试{
DatabaseReference=FirebaseDatabase.getInstance().getReference(“注释”);
addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
commentsList.clear();
对于(DataSnapshot快照:DataSnapshot.getChildren()){
ModelComments ModelComments=snapshot.getValue(ModelComments.class);
commentsList.add(modelComments);
}
commentAdapter.notifyDataSetChanged();
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}捕获(例外e){
e、 printStackTrace();
}
}

再说一次,有什么办法可以做到这一点吗?如果您不理解,请对此问题进行评论。

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

commentsList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
    ModelComments modelComments = snapshot.getValue(ModelComments.class);
    commentsList.add(modelComments);
}
commentAdapter.notifyDataSetChanged();
首先,清除列表,然后添加注释,最后调用notifyDataSetChanged。您错过的是在通知数据更改之前对列表进行排序

请检查此链接,了解如何对列表进行排序。

对列表进行排序,然后将其绑定到适配器,但如何绑定?