簡體   English   中英

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

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

我正在使用 recyclerview 制作項目。 當我將此語句添加到適配器時:

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

它拋出一個java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

編碼:

看起來您的帖子主要是代碼; 請添加更多詳細信息。 看起來您的帖子主要是代碼; 請添加更多詳細信息。

我也添加模型類來發布。

我不知道是什么原因造成這個問題。

   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
)

有人可以幫我解決這個問題嗎?

問題是 - 您正在訪問內部數組或內部項目列表。

你回來了 (cartItems.count())

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

對於綁定函數,您正在傳遞數據來自-cartItems[position].cartdocs

在綁定函數中,您正在訪問data.options[adapterPosition]

上面一行的工作原理類似於-cartItems[position].cartdocs.options[adapterPosition]

所以這里adapterPositiondata.options列表大小不匹配

如果需要,您可以將 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

請在綁定函數中試試這個。 如果您仍然遇到問題,請在評論部分告訴我。 我很樂意提供幫助。

快樂編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM