【问题标题】:How can i get list of comments at once from this JSON and pass it to recyclerview adapter如何从这个 JSON 中一次获取评论列表并将其传递给 recyclerview 适配器
【发布时间】:2020-03-14 18:03:19
【问题描述】:

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

我创建了成功案例模型类,其中我将 cmets 模型作为一个内部类包含了一些我想要顶部显示的属性,但最后它只显示评论正文的最后一个元素,而不是显示整个评论为 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


    }
}

```

【问题讨论】:

  • 能否提供你已经写好的代码?
  • 您正在使用的邮政编码,我希望您已经添加了用于在列表中添加评论的循环,并且该列表是在循环外创建并在循环内添加元素。
  • 你的问题出在你的适配器上。
  • 我能得到解决方案吗

标签: android json android-recyclerview retrofit retrofit2


【解决方案1】:

尝试在您的适配器中传递List&lt;CommentModel&gt; 并相应地实施。检查以下:

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
    }
}

【讨论】:

  • 我们总是可以在这些事情中使用 Gson,因为 Gson 会直接给出所需的数据模型
  • @ROHITLIEN,我知道。我个人也使用它。但我不知道他是否熟悉图书馆。这就是我添加基本流程的原因。后来他发布了模型,我看到他使用了它。感谢您的回复。
  • @darshankomu,尝试根据我的回答更新您的适配器并通过List&lt;CommentModel&gt; 而不是List&lt;SuccessStoryModel&gt; 并检查。让我知道更新
  • 顺便感谢@Md 的帮助。 Asaduzzaman 我得到的解决方案错误是 List 或 List 我使用 List 如果有人对此问题有任何其他更好的解决方案,那么他们是最受欢迎的
  • 其实List&lt;CommentModel&gt;List&lt;SuccessStoryModel.CommentModel&gt;是一样的。由于您使用的是内部类,因此您必须像这样编写 parentClass.innerClass
【解决方案2】:

解决方案可能在您的 recyclerview 适配器中,您必须处理每个元素

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多