我正在尝试获取特定帖子的两条评论,但 RecyclerView 只显示一条评论。
这是我在 Postman 上的 Api 调用测试。
但是当我在我的应用程序上使用这个调用时它只显示一条评论
代码如下
双注释适配器:
class TwoCommentsAdapter (var twocommentsList : ArrayList<Comments>) : RecyclerView.Adapter<TwoCommentsAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.two_comment, parent,false)
return ViewHolder(itemView)
}
override fun getItemCount(): Int {
return twocommentsList.count()
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.comment?.setText(twocommentsList.get(position).comment)
holder.username?.setText(twocommentsList.get(position).username)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var comment : TextView? = null
var username : TextView? = null
init {
this.comment = itemView.findViewById(R.id.comment_txt)
this.username = itemView.findViewById(R.id.comment_user_txt)
}
}
}
波科:
class Comments(val comment_id : Int, val username : String, val comment: String)
API 调用:
@FormUrlEncoded
@POST("/api/getTwoComment")
fun getTwoComments(@Field("post_id") post_id: Int): Call<ArrayList<Comments>>
Activity 中的改造调用:
getMainApp().swiftAPI.getTwoComments(post_id).enqueue(object : Callback<ArrayList<Comments>>{
override fun onFailure(call: Call<ArrayList<Comments>>?, t: Throwable?) {
Toast.makeText(this@ViewSinglePostActivity, t?.message, Toast.LENGTH_SHORT)
}
override fun onResponse(call: Call<ArrayList<Comments>>?, response: Response<ArrayList<Comments>>?) {
if (response?.isSuccessful!!){
val adapter = TwoCommentsAdapter(response.body()!!)
two_comments_rcv.adapter = adapter
}
}
})
提前致谢。如果你想要任何东西,请提供
最佳答案
而不是这个
override fun getItemCount(): Int {
return twocommentsList.count()
}
使用这个
override fun getItemCount(): Int {
return twocommentsList.size
}
一些其他常见问题是为项目布局设置了错误的高度,您可以看到您的 View 是否滚动,要修复检查 this
关于android - Recyclerview 只显示一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493112/