Ignore Event Handlers When Clearing a RecyclerView

Nathan Sokalski 3,976 Reputation points

I have a RecyclerView whose ViewHolder contains an EditText. In OnBindViewHolder in my Adapter, I add a TextChanged event handler to this, and then remove the handler in OnViewRecycled. However, one of the things I need to do at certain points is replace all the items in the RecyclerView. To do this, I need to clear the List & add the items from another List (by calling the Clear() & AddRange() methods), followed by calling NotifyDataSetChanged(). However, because of the recycling capabilities of RecyclerView, I receive a NullReferenceException in the TextChanged event handler. I do not actually need the TextChanged event handler at this point, but I'm not sure where to remove it for this scenario (like I said, I do remove it in OnViewRecycled). What should I do? Thanks.

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,820 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Nathan Sokalski 3,976 Reputation points

    I think I found a solution. Even though the ViewHolder is being removed from view, the Text property of the EditText was still being changed afterwards, therefore still triggering the TextChanged event, so I just removed the handler BEFORE deleting the item, so that solved the problem. I don't know if this will be a solution for everybody's scenario, but it seems to have solved it for me.

    No comments