java.lang.IndexOutOfBoundsException:索引:2,大小:微调项选择中的 2
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 in Spinner Item Selection
我尝试点击 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)
}
}
}
我的例外是:-
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at java.util.ArrayList.get(ArrayList.java:437) at com.maruti.marutisabji.adapter.user.ProductAdapter$onBindViewHolder.onItemSelected(ProductAdapter.kt:107) at android.widget.AdapterView.fireOnSelected(AdapterView.java:944) at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:933) at android.widget.AdapterView.access0(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)
这是我的代码Link请检查:- https://gist.github.com/maulikdadhaniya/c0e16a9a13d03e9b7abe2235a35e7324
您的代码必须以 else 块结尾
我建议在下面的代码块中避免此类 IndexOutOfBound
错误:
我们可以用户 AdapterView
根据位置找出所选项目,而不是直接从数组访问实体。
在您的 onItemSelected()
侦听器方法中,
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
// We can retrive selected item from adapter using below approach, so that it never give out of bound error.
selectedQty = parent?.getItemAtPosition(position) as? String
// Your rest of the code of else-if ladder also seems miscellaneous to me
}
将 onItemSelected
position
的第 3 个参数名称更改为 pos
。因为 position
已经在你的 onBindViewHolder
中使用,当你使用 items[position]
时它使用 position
of onItemSelected
但你必须使用 position
参数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)
}
}