[英]I am getting these errors while running kotlin project
I am getting these errors while running the application.我在运行应用程序时遇到这些错误。 Can someone help to fix the bug.
有人可以帮助修复错误。
Process: app.errandel.android, PID: 29755
java.lang.IndexOutOfBoundsException: Index: 25, Size: 25
You have two lists of data to pull from, but you arbitrarily pull from both lists using the position
, which may be bigger than either of them since getItemCount()
returns the combined size of both lists.您有两个要从中提取的数据列表,但是您可以使用
position
从两个列表中任意提取,这可能比其中任何一个都大,因为getItemCount()
返回两个列表的组合大小。
You need to decide which list to pull from based on how big the position is.您需要根据 position 的大小来决定从哪个列表中提取。 Something like this:
像这样的东西:
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
//...
}
}
Since you seem to be using a common layout for both types of list item, you will also need to possibly hide and show parts of the layout in the two respective branches of this if/else statement.由于您似乎对两种类型的列表项都使用了通用布局,因此您还可能需要在 if/else 语句的两个相应分支中隐藏和显示部分布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.