Recyclerview仅显示一个阵列

时间:2019-02-02 12:32:37

标签: android api retrofit

我正在尝试获取特定帖子的两条评论,但RecyclerView仅显示一条评论。

这是在我的邮差API调用的测试。

Retrofit

但是当我在我的应用上使用此通话时,它仅显示一条评论

下面是以下代码

TwoCommentAdapter:

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

改造呼叫中的活动:

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

        })

预先感谢。如果你想什么病提供

1 个答案:

答案 0 :(得分:1)

代替此

 override fun getItemCount(): Int {
     return twocommentsList.count()
 }

使用此

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

另一个常见的问题是为项目布局设置了错误的高度,您可以查看视图是否滚动,对于修复this的修复