Avoiding NullReferenceException with RecyclerView

Nathan Sokalski 4,116 Reputation points
2021-05-16T05:15:54.48+00:00

When using RecyclerView, the recycling feature is constantly causing methods such as FindViewHolderForAdapterPosition to return null, causing a NullReferenceException. This occurs in almost every scenario that involves more items than fit on screen (which is almost always, and it is the reason for having the recycling feature), and I cannot find a simple solution to avoid it. I need users to be able to scroll the RecyclerView, and sometimes I need to scroll it programmatically. Many of my scenarios involve editing and/or navigating data that is saved in collections & arrays and accessed by index, so I am often scrolling to items that are most likely already recycled. How can I easily access not currently visible items by index and/or know how & when to wait for them?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,254 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-05-17T08:20:31.363+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Many of my scenarios involve editing and/or navigating data that is saved in collections & arrays and accessed by index
    How can I easily access not currently visible items by index and/or know how & when to wait for them?

    We can only edit or navigate the item on the UI when the item is visible. Of cource, we can also edit the data in code-behind and call the following code in your RecyclerView.Adapter to update the UI once the item is visible.

              NotifyDataSetChanged();  
    

    For example, we can edit the datalist filled into the RecyclerView in our activity like this:

        private void updateData() {  
            Photo item = datalist[0]; // loadBranch is my datalist Photo  is my model of data list  
            item.mCaption = "updated value";  // mCaption  is a field of my item  
            mAdapter.UpDateData(datalist );//mAdapter is my RecyclerView's adapter     
        }  
    

    method UpDateData is a method of my RecyclerView.Adapter

        public class PhotoAlbumAdapter: RecyclerView.Adapter  
        {  
            public  List<Photo> mPhotoAlbum = new List<Photo>();  
      
            public  RecyclerView.Adapter adapter;  
      
            public PhotoAlbumAdapter(List<Photo> branchesList)  
            {  
                adapter = this;  
                mPhotoAlbum = branchesList;  
            }  
      
            public override void OnViewRecycled(Java.Lang.Object holder)  
            {  
                base.OnViewRecycled(holder);  
      
                PhotoViewHolder holder1 = holder as PhotoViewHolder;  
      
            }  
      
            public void UpDateData(List<Photo> temp)  
            {  
                mPhotoAlbum = temp;  
                NotifyDataSetChanged();  
            }  
      
       // other code  
       }  
    

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.