Search code examples
androidkotlinspinnerindexoutofboundsexception

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 in Spinner Item Selection


I try to click on Spinner item and when i click on 1 Kg it crashed and error give

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

Here is my code for Spinner and It listner :-

Here is my static array :-

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

set ArrayAdapter :-

 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
    }

and this is Listner of this spinner:-

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

            }
        }

My Exception is :-

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)

This is My code Link Please check it:- https://gist.github.com/maulikdadhaniya/c0e16a9a13d03e9b7abe2235a35e7324


Solution

  • Change your 3rd parameter name of onItemSelected position to pos. Because position is already used in your onBindViewHolder and when you use items[position] it use position of onItemSelected but you have to use the position parameter of onBindViewHolder.

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