There is a method in which I process a click on an element:
override fun onListItemClick(itemIndex: Int, itemCode: String) { presenter.onItemClick(adapter.getItem(itemIndex)) } But the problem is that the user somehow managed to click on the -1 element. What gave me the error:
"backtrace" => "[\" java.lang.ArrayIndexOutOfBoundsException: length \ u003d25; index \ u003d-1 \ ", \" at java.util.ArrayList.get (ArrayList.javaUE10) \ ", \" at com.project.android.features.common.base_list.AbstractListAdapter.getItem (AbstractListAdapter.kt : 20) \ ", \" at com.project.android.features.categories.selector.CategorySelectDialog.onListItemClick (CategorySelectDialog.kt: 59) \ ", \" at com.project.android.features.categories.selector.CategorySelectItemViewHolder $ 1.onClick (CategorySelectItemViewHolder.kt: 32) \ ", \" at android.view.View.performClick (View.java:4856) \ ", \" at android.view.View $ PerformClick.run (View.java: 19956) \ ", \" at android.os.Handler.handleCallback (Handler.java:739) \ ", \" at android.os.Handler.dispatchMessage (Handler.java:95) \ "," at android. os.Looper.loop (Looper.java:211) \ ", \" at android.app.ActivityThread.main (ActivityThread.java:5371) \ ", \" at java.lang.reflect.Method.invoke (Native Method ) \ ", \" at java.lang.reflect.Method.invoke (Method.java=72) \ ", \" at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:945) \ ", \" at com.android.in ternal.os.ZygoteInit.main (ZygoteInit.java:740) \ "]",
How is this possible? And how to exclude the moment when the user clicks on the -1 list item?
In one of the places where I use this method:
titleView.setOnClickListener { listener?.onListItemClick(adapterPosition, "") } I switched to the implementation of adapterPosition and saw the following:
/ ** * Returns the Adapter by the ViewHolder. *
If you’re not happy, you’ll not be able to do this. *
* RecyclerView doesn’t handle the next layout traversal. This can be a temporary, inconsistencies and adapter contents have. This is not the case since it will be less than 16ms. Sometimes, you may need to make it. In this case, you should calculate the position of the ViewHolder. *
* Note that if you’ve called {@link RecyclerView.Adapter # notifyDataSetChanged ()}, until the * next layout pass, you will {@link #NO_POSITION}. If it still exists in the adapter. * {@link RecyclerView # NO_POSITION} if the item has been removed from the adapter, * {@link RecyclerView.Adapter # notifyDataSetChanged ()} has the name has been already recycled. * /
Maybe somewhere there is a problem? So how exactly is this method returning -1.
public final int getAdapterPosition() { if (mOwnerRecyclerView == null) { return NO_POSITION; } return mOwnerRecyclerView.getAdapterPositionFor(this); }
if (itemIndex < 0) return;. And in order to understand how it happened, you need to look at the code that callsonListItemClick. - eugeneek