android - java.lang.IndexOutOfBoundsException : Index: 2, 大小:微调项选择中的 2

标签 android kotlin spinner indexoutofboundsexception

我尝试点击 Spinner 项目,当我点击 1 Kg 时,它崩溃了,并给出了错误

Blockquote java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

这是我的 Spinner 和 It listner 代码:-

这是我的静态数组:-

 val values = arrayOf(
        "250 gm",
        "500 gm",
        "1 Kg",
        "2 Kg",
        "3 Kg",
        "4 Kg", "5 Kg", "6 Kg", "7 Kg"
    )

设置数组适配器:-

 ArrayAdapter(
        context,
        android.R.layout.simple_spinner_item,
        values
    ).also {
        it.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        holder.product_spinner_kg.adapter = it
    }

这是这个微调器的 Listner:-

holder.product_spinner_kg.onItemSelectedListener =
        object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?, view: View?, position: Int, id: Long
            ) {


                selectedQty = values[position]

                if (position == 0) {
                    holder.product_price.text =
                        "Rs. " + (items[position].price.toDouble() * 0.25)
                } else if (position == 1) {
                    holder.product_price.text =
                        "Rs. " + (items[position].price.toDouble() * 0.50)
                } else if (position == 2) {
                    holder.product_price.text =
                        "Rs. " + (items[position].price.toDouble() * 1.00)
                } else if (position == 3) {
                    holder.product_price.text =
                        "Rs. " + (items[position].price.toDouble() * 2.00)
                } else if (position == 4) {
                    holder.product_price.text =
                        "Rs. " + (items[position].price.toDouble() * 3.00)
                }

            }
        }

我的异常(exception)是:-

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at java.util.ArrayList.get(ArrayList.java:437) at com.maruti.marutisabji.adapter.user.ProductAdapter$onBindViewHolder$3.onItemSelected(ProductAdapter.kt:107) at android.widget.AdapterView.fireOnSelected(AdapterView.java:944) at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:933) at android.widget.AdapterView.access$300(AdapterView.java:53) at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:898) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6820) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)

这是我的代码链接请检查:- https://gist.github.com/maulikdadhaniya/c0e16a9a13d03e9b7abe2235a35e7324

最佳答案

onItemSelected position 的第三个参数名称更改为 pos。因为 position 已经在你的 onBindViewHolder 中使用了,当你使用 items[position] 它使用 positiononItemSelected 但你必须使用 onBindViewHolderposition 参数。

override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long){

    selectedQty = values[pos] // here you should use pos

    if (pos == 0) { // need to chnage also here and below conditions
        holder.product_price.text = "Rs. " + (items[position].price.toDouble() * 0.25)
    } else if (pos == 1) {
        holder.product_price.text = "Rs. " + (items[position].price.toDouble() * 0.50)
    } else if (pos == 2) {
        holder.product_price.text = "Rs. " + (items[position].price.toDouble() * 1.00)
    } else if (pos == 3) {
        holder.product_price.text = "Rs. " + (items[position].price.toDouble() * 2.00)
    } else if (pos == 4) {
        holder.product_price.text = "Rs. " + (items[position].price.toDouble() * 3.00)
    }
}

关于android - java.lang.IndexOutOfBoundsException : Index: 2, 大小:微调项选择中的 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58968120/

相关文章:

安卓工作室 : open file operation failed (The open file operation failed to connect to the open and save panel service. )

java - 迁移到 v28.0.0 支持库后 Android gradle 构建失败

java - 如何编写并发单元测试

apache-flex - 显示旋转的 AJAX 加载器图像

java - 计算图像重叠百分比

java - 错误: android. widget.LinearLayout 无法转换为 android.widget.ImageView

android - Android 上的 kivy/Python 缺少 Unicode 编解码器?

AndroidX房间数据库

android - RetryWhen与Single一起使用时出现一些问题

java - Android Spinner 未显示