简体   繁体   English

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin

[英]java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin

I'm making project using recyclerview.我正在使用 recyclerview 制作项目。 When I add this statement to adapter:当我将此语句添加到适配器时:

itemView.optionText.text=data.options[adapterPosition].description

It throws a java.lang.IndexOutOfBoundsException: Index: 1, Size: 1它抛出一个java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

The code:编码:

It looks like your post is mostly code;看起来您的帖子主要是代码; please add some more details.请添加更多详细信息。 It looks like your post is mostly code;看起来您的帖子主要是代码; please add some more details.请添加更多详细信息。

I aslo add model class to post.我也添加模型类来发布。

I dont know what reason make this problem.我不知道是什么原因造成这个问题。

   internal class CartItemRecyclerAdapter(var context: Context, var cartItems: ArrayList<ViewCartdocs>,  var listener: CartItemListener) : RecyclerView.Adapter<CartItemRecyclerAdapter.Holder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
            var view = LayoutInflater.from(context).inflate(R.layout.cart_item_list, parent, false)
            return Holder(view)
        }

        override fun onBindViewHolder(holder: Holder, position: Int) {

             holder.bind(cartItems[position].cartdocs, context)
            holder.checkbox.isChecked=cartItems[position].selected

            holder.checkbox.setOnCheckedChangeListener(null)

            holder.checkbox.setOnCheckedChangeListener { _, isChecked ->
                if (isChecked) {
                    holder.checkbox.background = holder.checkbox.context.getDrawable(R.drawable.check_box_active_cs)
                } else {
                    holder.checkbox.background = holder.checkbox.context.getDrawable(R.drawable.check_box_no)
                    }
                    listener.onCheckboxChanged(position, isChecked)
            }

}

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

            inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {

            fun bind(data: Cartdocs, context: Context) {
            val dec = DecimalFormat("##,000")
            itemView.titleText.text = data.title
            itemView.priceText.text = dec.format(data.price).toString() 
            itemView.textViewItemNumer.text = data.amount.toString()
            itemView.pointValueText.text = dec.format(data.point).toString() + "P"
            itemView.optionText.text= data.options[adapterPosition].description
            Glide.with(context).load(data.mainImage).into(imageView)

            } } } }

data class Cartdocs(
    @SerializedName("id")
    var id:String?=null,
    @SerializedName("title")
    var title:String?=null,
    @SerializedName("stock")
    var stock:Int?=null,
    @SerializedName("mainImage")
    var mainImage:String?=null,
    @SerializedName("amount")
    var amount:Int?=null,
    @SerializedName("added")
    var added:String?=null,
    @SerializedName("options")
    var options:ArrayList<cartOptions>,
    var unitPoint:Int?=null,
    @SerializedName("totalPrice")
    var totalPrice:Int?=null,
    @SerializedName("totalPoint")
    var totalPoint:Int?=null
)


data class cartOptions(

    var id:String?=null,

    var description:String?=null
)

Can someone please help me with this?有人可以帮我解决这个问题吗?

The problem is - You are accessing inner array or inner list of items.问题是 - 您正在访问内部数组或内部项目列表。

You are returning (cartItems.count())你回来了 (cartItems.count())

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

For bind function you are passing data from - cartItems[position].cartdocs对于绑定函数,您正在传递数据来自-cartItems[position].cartdocs

Inside bind function you are accessing data.options[adapterPosition]在绑定函数中,您正在访问data.options[adapterPosition]

above line works like - cartItems[position].cartdocs.options[adapterPosition]上面一行的工作原理类似于-cartItems[position].cartdocs.options[adapterPosition]

So here adapterPosition not match with data.options list size所以这里adapterPositiondata.options列表大小不匹配

If you need, you can combine ArrayList of cartOptions as a single string如果需要,您可以将 carOptions 的 ArrayList 组合为单个字符串

var descriptions = ""

for ((index, option) in data.options.withIndex()) {
    descriptions += option.description

    if (index != (data.options.size - 1)) {
        descriptions += ", "
    }
}

itemView.optionText.text= descriptions

Please try this inside bind function.请在绑定函数中试试这个。 If still you are facing the problem please let me know in the comments section.如果您仍然遇到问题,请在评论部分告诉我。 I am happy to help.我很乐意提供帮助。

Happy Coding :)快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM