question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Ignore Event Handlers When Clearing a RecyclerView

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.

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

However, because of the recycling capabilities of RecyclerView, I receive a NullReferenceException in the TextChanged event handler.

Hi @njsokalski , it is difficult for us to locate the problem based only on your description. Could you please post some code snippets or a basic demo so that we can test on our side?
0 Votes 0 ·

1 Answer

njsokalski avatar image
0 Votes"
njsokalski answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.