title: Updating RecyclerView in a Fragment - Xamarin Android
body: <p>I have a 3 fragments on my view pager. On my 3rd fragment is a recyclerViewer which contains contact list. Now, i am finding it difficult as to how to update my contact list (recycleViewer with data) from my database. <code>recycleview</code> in Fragment.axml is inflated from Fragment 3.cs.</p>
  
  <p><strong>Fragment 3.cs</strong></p>
  
  <pre><code>    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
              {
  
                  RecyclerView recyclerView = inflater.Inflate(Resource.Layout.Fragment3, container, false) as RecyclerView;
                  implementRecyclerView(recyclerView);
  
                  return recyclerView;
              }
  
  private void SetUpRecyclerView(RecyclerView recyclerView)
          {        
  
              recyclerView.SetLayoutManager(new LinearLayoutManager(recyclerView.Context));
  
          recyclerView.SetAdapter(Adapter);
  
              recyclerView.Click += eventClicked;
  
              Adapter.NotifyDataSetChanged();
  
              recyclerView.SetItemClickListener((rv, position, view) =&gt;
              {
                  //An item has been clicked
                  Context context = view.Context;
                  Intent intent = new Intent(context, typeof(EventDetailActivity));
  
                  context.StartActivity(intent);
              });
  
          }
  </code></pre>
  
  <p>This is where my <code>RecycleView</code> that actually displays the contact list after being inflated in the <code>Fragment 3.cs</code></p>
  
  <p><strong>Fragment 3 axml</strong></p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
  
  //id of RecycleView 
          android:id="@+id/recyclerview"
  
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  </code></pre>
  
  <p>In this activity, I add some data to my database. After adding, i call the <code>RefreshListWithNewData()</code> to update my RecyclerView (contact list). I keep getting <code>NullReference</code> trying to get the id of the RecycleView. 
  <strong>SomeActivity.cs</strong></p>
  
  <pre><code>JavaList&lt;Contact&gt; myContactList = new JavaList&lt;Contact&gt;();
  
  
  private async void AddNewGames(){
  
  //add contacts to database 
  
  await RefreshListWithNewData()
  
  
  }
  
  
  private async Task RefreshListWithNewData(){
  
                  foreach(var item in items){
                  Contact people = new Contact();
                  people.Name = item.Object.Name;
                  myContactList.Add(people);
  
  
  
  }
  
   //trying to update recycleview in Fragment 3.cs
  refreshRecycler = (RecyclerView)FindViewById(Resource.Id.recycleview)
  adapter = new ContactAdapter(myContactList);
   refreshRecycler.SetAdapter(adapter);
  </code></pre>
  
  <p><strong>My question is why can't i reach that RecycleView even after i have given it an id called <code>recycleview</code>?</strong></p>
  
  <p><strong>Updated Fragment3.cs</strong></p>
  
  <pre><code>public class Fragment3 : SupportFragment
  
  
  
  {
      public RecyclerView refresh;
  
  
  
  
      public override void OnCreate(Bundle savedInstanceState)
      {
          base.OnCreate(savedInstanceState);
  
  
      }
  
  
  
      public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
  
  
          RecyclerView recyclerView = inflater.Inflate(Resource.Layout.Fragment3, container, false) as RecyclerView;
  
  
          SetUpRecyclerView(recyclerView);
  
  
  
  
          return recyclerView;
  
  
  
      }
  
  
  
  
      private void SetUpRecyclerView(RecyclerView recyclerView)
      {
  
          //var value= GetRandomSubList(PeaceEventsCollection.GetPeaceEvents(), 5);
          var value = PeaceEventsCollection.GetPeaceEvents();
  
          recyclerView.SetLayoutManager(new LinearLayoutManager(recyclerView.Context));
  
  
  
  
  
          var Adapter = new EventAdapter(PeaceEventsCollection.GetPeaceEvents());
  
  
  
  
  
  
      recyclerView.SetAdapter(Adapter);
  
          recyclerView.Click += eventClicked;
  
          Adapter.NotifyDataSetChanged();
  
          recyclerView.SetItemClickListener((rv, position, view) =&gt;
          {
              //An item has been clicked
              Context context = view.Context;
              Intent intent = new Intent(context, typeof(EventDetailActivity));
              intent.PutExtra(EventDetailActivity.EXTRA_NAME,  value[position].EName);
              intent.PutExtra(EventDetailActivity.EXTRA_DETAIL, value[position].EDescritption);
              intent.PutExtra(EventDetailActivity.EXTRA_DATE, value[position].EDate);
  
  
  
              //perform activity to event page
              context.StartActivity(intent);
          });
  
  
  
      }
  
  
      private JavaList&lt;PeaceEvents&gt; GetRandomSubList(JavaList&lt;PeaceEvents&gt; items, int amount)
      {
          JavaList&lt;PeaceEvents&gt; list = new JavaList&lt;PeaceEvents&gt;();
          Random random = new Random();
          while (list.Count &lt; amount)
          {
              list.Add(items[random.Next(items.Count)]);
          }
  
          return list;
      }
  
      private void eventClicked(object sender, EventArgs e)
      {
          Intent showEvent = new Intent(Context, typeof(EventActivity));
          this.StartActivity(showEvent);
      }
  
      public class EventAdapter : RecyclerView.Adapter
      {
  
          private JavaList&lt;PeaceEvents&gt; peaceEvents;
  
          public EventAdapter(JavaList&lt;PeaceEvents&gt; peaceEvents)
          {
              this.peaceEvents = peaceEvents;
  
  
          }
  
  
          public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
          {
  
              EventHolder hold = holder as EventHolder;
              hold.EventName.Text = peaceEvents[position].EName;
              hold.EventImg.SetImageResource(peaceEvents[position].EImage);
              hold.EventDate.Text = peaceEvents[position].EDate;
              hold.EventVenue.Text = peaceEvents[position].EVenue;
              hold.EventDescription.Text = peaceEvents[position].EDescritption;
  
          }
  
  
          public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
          {
              View v = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.List_Event, parent, false);
              EventHolder holder = new EventHolder(v);
  
              return holder;
          }
  
          public override int ItemCount
          {
  
              get { return peaceEvents.Size(); }
  
          }
  
  
      }
  
  }
  
  
  
  
  public class EventHolder : RecyclerView.ViewHolder
  {
  
      public TextView EventName;
      public ImageView EventImg;
      public TextView EventDate;
      public TextView EventVenue;
      public TextView EventDescription;
      public TextView textIcon;
      public TextView EventDetailDescription;
  
  
      public EventHolder(View itemView) : base(itemView)
      {
          EventName = itemView.FindViewById&lt;TextView&gt;(Resource.Id.textViewEvent);
          EventImg = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.imageView);
          EventDate = itemView.FindViewById&lt;TextView&gt;(Resource.Id.textViewDate);
          EventVenue = itemView.FindViewById&lt;TextView&gt;(Resource.Id.textViewVenue);
          EventDescription = itemView.FindViewById&lt;TextView&gt;(Resource.Id.textViewDescription);
  
  
  
  
  
  
      }
  
  }
  </code></pre>
  
  <p><strong>EventActivity</strong></p>
  
  <pre><code> public class EventActivity : AppCompatActivity
  
  
      {
  
          private EditText EventName;
          private EditText EventDateTime;
          private EditText EventVenue;
          private EditText EventDescription;
          private ProgressBar circular_progress;
          private Button saveData;
          private LinearLayout list_view;
          private RecyclerView recycle;
          // private EventAdapter adapter;
          JavaList&lt;PeaceEvents&gt; peace_Events = new JavaList&lt;PeaceEvents&gt;();
  
  
          //  private PeaceEvents selectedEvents;
  
          private const string FirebaseUrl = "";
  
  
  
  
  
          private DrawerLayout mDrawerLayout;
  
  
  
          protected override void OnCreate(Bundle savedInstanceState)
          {
              base.OnCreate(savedInstanceState);
  
              SetContentView(Resource.Layout.Events);
              // Create your application here
              SupportToolbar toolBar = FindViewById&lt;SupportToolbar&gt;(Resource.Id.toolBar);
  
              SetSupportActionBar(toolBar);
  
              //view 
  
              circular_progress = FindViewById&lt;ProgressBar&gt;(Resource.Id.circularProgress);
              EventName = FindViewById&lt;EditText&gt;(Resource.Id.eventName);
              EventDateTime = FindViewById&lt;EditText&gt;(Resource.Id.txtDateAndTime);
              EventVenue = FindViewById&lt;EditText&gt;(Resource.Id.venue);
              EventDescription = FindViewById&lt;EditText&gt;(Resource.Id.txtDescribe);
              list_view = FindViewById&lt;LinearLayout&gt;(Resource.Id.list_view);
  
              saveData = FindViewById&lt;Button&gt;(Resource.Id.btnLogin);
  
  
              saveData.Click += saveEventData;
  
  
  
  
  
  
  
  
  
              SupportActionBar ab = SupportActionBar;
              ab.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
              ab.SetDisplayHomeAsUpEnabled(true);
  
              mDrawerLayout = FindViewById&lt;DrawerLayout&gt;(Resource.Id.drawer_layout);
  
              NavigationView navigationView = FindViewById&lt;NavigationView&gt;(Resource.Id.nav_view);
              if (navigationView != null)
              {
                  SetUpDrawerContent(navigationView);
              }
  
          }
  
          private void saveEventData(object sender, EventArgs e)
          {
              CreateEvent();
          }
  
          private async void CreateEvent()
          {
              PeaceEvents events = new PeaceEvents();
              events.EName = EventName.Text;
              events.EDate = EventDateTime.Text;
              events.EVenue = EventVenue.Text;
              events.EDescritption = EventDescription.Text;
  
              var firebase = new FirebaseClient(FirebaseUrl);
  
              //add item 
  
              var item = await firebase.Child("").PostAsync&lt;PeaceEvents&gt;(events);
  
              await LoadData();
  
  
          }
  
          private  async Task LoadData()
          {
              circular_progress.Visibility = ViewStates.Visible;
  
              var firebase = new FirebaseClient(FirebaseUrl);
              var items = await firebase.Child("").OnceAsync&lt;PeaceEvents&gt;();
  
              peace_Events.Clear();
  
              EventAdapter adpater;
  
  
  
  
              foreach(var item in items){
                  PeaceEvents events = new PeaceEvents();
                  events.EName = item.Object.EName;
                  events.EDate = item.Object.EDate;
                  events.EDescritption = item.Object.EDescritption;
                  events.EVenue = item.Object.EVenue;
                  events.EImage = item.Object.EImage;
  
                  peace_Events.Add(events);
  
  
  
  
              }
  
             // Fragment3.UpdateContactList&lt;Ja&gt;(peace_Events);
  
              //adpater = new EventAdapter(peace_Events);
              ////recycle.SetAdapter(adpater);
              ////recycle.SetLayoutManager(new LinearLayoutManager(this));
              //adpater.NotifyDataSetChanged();
  
  
              //recycle.SetAdapter(adpater);
  
  
  
  
  
  
              circular_progress.Visibility = ViewStates.Invisible;
  
  
  
          }
  
          public override bool OnOptionsItemSelected(IMenuItem item)
          {
  
  
  
  
              switch (item.ItemId)
              {
                  case Android.Resource.Id.Home:
                      mDrawerLayout.OpenDrawer((int)GravityFlags.Left);
                      return true;
  
  
                  default:
                      return base.OnOptionsItemSelected(item);
              }
          }
  
          private void SetUpDrawerContent(NavigationView navigationView)
          {
              navigationView.NavigationItemSelected += (object sender, NavigationView.NavigationItemSelectedEventArgs e) =&gt;
              {
                  int id = e.MenuItem.ItemId;
  
                  switch (id)
                  {
  
                      case Resource.Id.nav_home:
                          Intent home = new Intent(this, typeof(MainActivity));
                          this.StartActivity(home);
                          GC.Collect();
  
                          break;
  
                      case Resource.Id.nav_peacehero:
  
                          Intent intent = new Intent(this, typeof(PeaceFragement));
                          this.StartActivity(intent);
                          GC.Collect();
  
                          break;
  
                      //case Resource.Id.nav_discussion:
  
                      //    Intent discuss = new Intent(this, typeof(PeaceHeroActivity));
                      //    this.StartActivity(discuss);
  
                      //    break;
  
                      //case Resource.Id.nav_event:
  
                      //    Intent peace_event = new Intent(this, typeof(PeaceHeroActivity));
                      //    this.StartActivity(peace_event);
  
  
                      //    break;
  
                      //case Resource.Id.nav_donate:
  
                          //Intent donate = new Intent(this, typeof(PeaceHeroActivity));
                          //this.StartActivity(donate);
  
                          //break;
                  }
  
  
  
                  e.MenuItem.SetChecked(true);
                  mDrawerLayout.CloseDrawers();
              };
          }
      }
  }
  </code></pre>