Skip to content Skip to sidebar Skip to footer

Onclicklistener Is Not Working Inside My Adapter Class

I looked at many solutions posted online but they couldn't solve my problem. Probably the adapter position is returning -1 but why? java.lang.ArrayIndexOutOfBoundsException: le

Solution 1:

adapterPosition in the ViewHolder's init block is returning -1. So we just need to call adapterPosition in the listener which will solve the issue, since it would retrieve the position when the view holder is created and attached to the recyclerview. Setting any kind of listeners in the init block is a good approach since the onCreateViewHolder called only once to create a view holder but the onBindViewHolder called multiple times for the same view holder, so setting listeners in the onBindViewHolder would be redundant.

init {

            thumbsUp.setOnClickListener {
                val tweetId=snapshots.getSnapshot(adapterPosition).get("tweetId")
                clickInterface.clickLike(tweetId.toString())
            }

   }

Solution 2:

Attache you onClickListener in onBindViewHolder

overridefunonBindViewHolder(
            holder: TweetViewHolder,
            position: Int,
            model: Tweet
        ) {
            //your code of attaching data to view
            holder.thumbsUp.setOnClickListener{
              clickInterface.clickLike(model.tweetId.toString())
           }
         }

Reconsider your application architecture. As asking DAO for data inside RecyclerView.Adapter is quite strange approach

Solution 3:

Yeah, I'd expect adapterPosition in your getSnapshotcall in the ViewHolder's init block is returning -1. As far as I'm aware, all the adapter position getters return the position of the last requested item for display. But when you're first creating ViewHolders, nothing has bound to the adapter yet (it's all still in the setup phase), so its default position is probably -1.

Basically the way RecyclerViews work is this - you define these ViewHolder objects which are basically things that display data for each item. Instead of having one for each item in the list, the adapter creates a handful of them (enough to fill the visible area and a couple either side), and then recycles them by moving them around when they're out of view, and updates their contents to represent a different item.

onCreateViewHolder is what the adapter calls to create those container objects. It's where you inflate the layout and usually do findViewById on the views so you have a reference to them, and you can just set a TextView's text instead of having to fnd the view every time you want to update it.

onBindViewHolder is what gets called when you need to update a ViewHolder with a different item's info. This is where you actually do all the setting of data, changing images and whatever, updating click listeners if necessary.

Basically you shouldn't be using adapterPosition in your ViewHolder's init block, because that gets called once, when the object is constructed. Not only do you not have a meaningful position at that point, but isn't that something you'll want to update often, as the position changes? That's what onBindViewHolder is for! It has the position passed in and everything. That's the method where you set state

Post a Comment for "Onclicklistener Is Not Working Inside My Adapter Class"