OnClickItemListener в spinner в андроиде просмотра recycler

Я создаю элемент, где у меня есть только spinner:

class ItemSelect constructor(val list: ArrayList<String>) : AbstractItem<ItemSelect, ItemSelect.ViewHolder>() { override fun getType(): Int { return R.id.itemSpinner } override fun getLayoutRes(): Int { return R.layout.item_select } override fun bindView(holder: ViewHolder, payloads: MutableList<Any>?) { super.bindView(holder, payloads) val spinnerAdapter = ArrayAdapter<String>(holder.context, android.R.layout.simple_spinner_item) spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) spinnerAdapter.addAll(list) holder.spinner.adapter = spinnerAdapter holder.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { } override fun onNothingSelected(parent: AdapterView<*>) { } } } override fun unbindView(holder: ViewHolder) { super.unbindView(holder) } override fun getViewHolder(v: View): ViewHolder { return ViewHolder(v) } class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val spinner: Spinner = view.itemSpinner val context: Context = view.context } } 

Элемент добавляется к просмотру ресайклеров.

Когда я хочу показать это мнение, я получил ошибку:

java.lang.RuntimeException: Не вызывайте setOnClickListener для адаптера. Вероятно, вы, вероятно, хотите установить setOnItemClickListener

Ошибка:

 java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead at android.widget.AdapterView.setOnClickListener(AdapterView.java:808) at com.mikepenz.fastadapter.utils.EventHookUtil.attachToView(EventHookUtil.java:58) at com.mikepenz.fastadapter.FastAdapter.onCreateViewHolder(FastAdapter.java:608) at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6488) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5675) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5558) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5554) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3694) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3411) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3963) at android.view.View.layout(View.java:19586) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1780) at android.widget.LinearLayout.onLayout(LinearLayout.java:1546) at android.view.View.layout(View.java:19586) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19586) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443) at android.view.View.layout(View.java:19586) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19586) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19586) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:758) at android.view.View.layout(View.java:19586) at android.view.ViewGroup.layout(ViewGroup.java:6053) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911) at android.view.Choreographer.doCallbacks(Choreographer.java:723) at android.view.Choreographer.doFrame(Choreographer.java:658) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) 11-20 15:01:16.691 6942-6942/pl.skillforce.formsandroid E/AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Попробуй это

 yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(Spinner parent, View view, int position, long id) { } }); 

Ошибка ясно, что вам нужно использовать setOnItemClickListener, который возвращает вам индекс элемента, нажатого.

setOnClickListener для

Зарегистрируйте обратный вызов, который будет вызываться при нажатии этого вида. Если это представление не доступно для кликов, оно становится интерактивным.

и setOnItemClickListener

Зарегистрируйте обратный вызов для вызова, когда элемент в этом AdapterView был нажат.

поэтому постарайтесь:

 spinner.setOnItemClickListener( {parent, view, position, id -> "Item $position clicked" })