【发布时间】: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