java.lang.ClassCastException:java.util.HashMap不能转换为com.example.drivie.Models.Comment

java.lang.ClassCastException:java.util.HashMap不能转换为com.example.drivie.Models.Comment,java,android,Java,Android,我想从数据库中检索一些数据并显示在RecyclerView中 但当我运行应用程序时,它会向我抛出以下信息: 进程:com.example.**,PID:22231 java.lang.ClassCastException:java.util.HashMap不能强制转换为com.example.***.Models.Comment 在com.example.**.util.CommentAdapter.onBindViewHolder(CommentAdapter.java:39)上 在com.e

我想从数据库中检索一些数据并显示在RecyclerView中

但当我运行应用程序时,它会向我抛出以下信息:

进程:com.example.**,PID:22231 java.lang.ClassCastException:java.util.HashMap不能强制转换为com.example.***.Models.Comment 在com.example.**.util.CommentAdapter.onBindViewHolder(CommentAdapter.java:39)上 在com.example.***.util.CommentAdapter.onBindViewHolder(CommentAdapter.java:21)

我向适配器传递我猜的相同类型的数据,我的意思是我有一个ArrayList并传递它。让我给你看看

这是活动中的函数

private void initView() {
    RvComments.setLayoutManager(new LinearLayoutManager(this));

    db.collection("Users").whereEqualTo("email", email)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful())
                    {
                        for(QueryDocumentSnapshot document: task.getResult()) {

                             ArrayList<Comment> comments = (ArrayList<Comment>) document.get("comments");
                            commentAdapter = new CommentAdapter(getApplicationContext(),comments );
                            RvComments.setAdapter(commentAdapter);


                        }
                    }
                }
            });

}

在公共CommentAdapter(Context mContext,ArrayList mData)中,我希望有一个ArrayList,在我传递ArrayList的活动中的函数中,我不知道为什么会得到这个,也许你应该知道。你能看一下吗?提前谢谢你

你能发布包含“评论”标签的文档文本吗?arraylistcomments=(ArrayList)document.get(“comments”);看起来可能是序列化问题,您没有将注释序列化为注释对象。看起来你是在传递一个映射来代替你的评论。关于for循环,你的逻辑看起来也是错误的。您在每次迭代中都要替换适配器,而应该向当前适配器添加现有注释。这是我看到的另一个问题。您应该存储注释,然后在for循环中处理完所有注释后设置适配器。我理解,非常感谢!我会尽快修改并让你知道它是否有效

import java.util.ArrayList;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    private Context mContext;
    private ArrayList<Comment> mData;

    public CommentAdapter(Context mContext, ArrayList<Comment> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }
    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View row = LayoutInflater.from(mContext).inflate(R.layout.row_comments, parent,false);
       return new CommentViewHolder(row);
    }
    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {

        Glide.with(mContext).load(mData.get(position).profilePicBy).into(holder.img_user);
        holder.tv_name.setText(mData.get(position).postedBy);
        holder.tv_content.setText(mData.get(position).content);
        holder.tv_reply.setText(mData.get(position).reply);

    }
    @Override
    public int getItemCount() {
        return  mData.size();
    }
    public class  CommentViewHolder extends RecyclerView.ViewHolder {

        ImageView img_user;
        TextView tv_name,tv_content,tv_reply;
        Button reply;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            img_user = itemView.findViewById(R.id.comment_user_imd);
            tv_name = itemView.findViewById(R.id.comment_fullname);
            tv_content = itemView.findViewById(R.id.comment_content);
            tv_reply = itemView.findViewById(R.id.comment_reply);
            reply = itemView.findViewById(R.id.comment_postReply);

            reply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("This button works");
                }
            });
        }
    }
}

public class Comment {
    public String postedBy;
    public String content;
    public String emailPostedBy;
    public String profilePicBy;
    public String reply;
    public String id;

    public Comment() {

    }

    public Comment(String postedby, String content, String emailPostedBy, String profilePicBy, String reply, String id){
        this.postedBy = postedby;
        this.content = content;
        this.emailPostedBy = emailPostedBy;
        this.profilePicBy = profilePicBy;
        this.reply = reply;
        this.id = id;
    }
}