我如何立即从此 JSON 获取评论列表并将其传递给 recyclerview 适配器

How can i get list of comments at once from this JSON and pass it to recyclerview adapter

我可以从 JSON 获取评论正文,但它只显示每个评论正文的最后一个元素,而不是评论正文的完整列表,所以如果要显示评论正文,我如何获取列表它在 android 回收者视图

我创建了成功案例模型 class,其中我将评论模型作为内部 class 包含了一些属性,我希望在顶部显示但最后它只显示最后一个元素评论正文而不是将整个评论显示为 recyclerview 适配器上的列表

[
    {
        "id": "2",
        "pic": "https://amraenterprises.com/heenahealth/uploads/story/06112019110550newyear.jpg",
        "caption": "New Year's Day, also simply called New Year or New Year's, is observed on January 1, the first day of the year on the modern Gregorian calendar as well as the Julian calendar. ",
        "story_count": 3,
        "story_count_comment": 2,
        "comments": [
            {
                "commentid": "1",
                "postid": "2",
                "commenttext": "It's awesome. Thanks for giving us this information.",
                "userid": "4",
                "username": "lorem",
                "isreply": "false",
                "replyto": "-1",
                "type": "story"
            },
            {
                "commentid": "4",
                "postid": "2",
                "commenttext": "Always welcome :)",
                "userid": "1",
                "username": "admin",
                "isreply": "true",
                "replyto": "1",
                "type": "story"
            }
        ]
    }


]

我的适配器代码


class CommentAdapter(private val context: Context, private val successCommentModel: List<SuccessStoryModel>,private val mPos : String) : RecyclerView.Adapter<CommentAdapter.RecyclerViewAdapter>() {



    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RecyclerViewAdapter {

        val view = LayoutInflater.from(context).inflate(R.layout.custom_comment, viewGroup, false)

        return RecyclerViewAdapter(view)

    }

    override fun onBindViewHolder(holder: RecyclerViewAdapter, pos: Int) {

            if(successCommentModel[pos].comments != null)
            {
                if(successCommentModel[pos].comments!![pos].postid == mPos)
                {

                    for(i in 0 until successCommentModel[pos].comments!!.size )
                    {
                        holder.user_name.text = "${successCommentModel[i].comments!![pos].username}"

                        holder.user_comment.text = "${successCommentModel[i].comments!![pos].commenttext}"

                    }


                }
                else
                {
                    holder.user_comment.visibility = View.GONE
                    holder.user_name.visibility = View.GONE
                    holder.no_user_comment.visibility = View.GONE

                }
            }
            else
            {
                holder.user_comment.visibility = View.GONE
                holder.user_name.visibility = View.GONE
                holder.no_user_comment.visibility = View.VISIBLE

            }






    }

    override fun getItemCount(): Int {
        return successCommentModel.size
    }






}


这是成功案例和评论的模型代码

class SuccessStoryModel {



    @SerializedName("comments")
    @Expose
    var comments: List<CommentModel>? = null



   inner class CommentModel {

        @SerializedName("commentid")
        @Expose
        var commentid: String? = null
        @SerializedName("postid")
        @Expose
        var postid: String? = null
        @SerializedName("commenttext")
        @Expose


    }
}

```

尝试在您的适配器中传递 List<CommentModel> 并相应地实施。检查以下内容:

class CommentAdapter(private val context: Context, private val comments: List<CommentModel>) : RecyclerView.Adapter<CommentAdapter.RecyclerViewAdapter>() {



    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RecyclerViewAdapter {

        val view = LayoutInflater.from(context).inflate(R.layout.custom_comment, viewGroup, false)

        return RecyclerViewAdapter(view)

    }

    override fun onBindViewHolder(holder: RecyclerViewAdapter, pos: Int) {

        val comment = comments[pos]

        holder.user_name.text = "${comment.username}"
        holder.user_comment.text = "${comment.commenttext}"

    }

    override fun getItemCount(): Int {
        return comments.size
    }
}

解决方案可能在你的回收视图适配器中,你必须处理每个元素