我在运行Kotlin项目时遇到这些错误

时间:2020-04-27 14:08:21

标签: android kotlin

运行应用程序时出现这些错误。有人可以帮助修复该错误。

Process: app.errandel.android, PID: 29755
java.lang.IndexOutOfBoundsException: Index: 25, Size: 25

1 个答案:

答案 0 :(得分:1)

您有两个要提取的数据列表,但是您可以使用position从两个列表中任意提取数据,因为getItemCount()返回两个列表的总大小,所以它们可能大于其中两个。

您需要根据职位的大小来决定从哪个列表中选择。像这样:

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    if (position < feedback.size) {
        val feedbackItem = feedback[position]
        itemView.tv_question.text = feedbackItem.questions
        //...
    } else {
        val answersItem = answerss[position - feedback.size]
        holder.itemView.cb_answer.text = answersItem
        //...
    }

}

由于两种类型的列表项似乎都使用相同的布局,因此您还可能需要在if / else语句的两个分支中隐藏和显示布局的一部分。