简体   繁体   中英

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

I'm making project using 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

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

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

For bind function you are passing data from - cartItems[position].cartdocs

Inside bind function you are accessing data.options[adapterPosition]

above line works like - cartItems[position].cartdocs.options[adapterPosition]

So here adapterPosition not match with data.options list size

If you need, you can combine ArrayList of cartOptions as a single string

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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