title: Updating ListView in Fragment - Xamarin.Android
body: <p>I'm new to Android dev, so it's really weird for me what's wrong in this code.</p>
  
  <p>I've spent a lot of time in xamarin forums and finally I gave up, so asking your help.</p>
  
  <pre><code>namespace App1.Fragments
  {
      public class HomePage : Android.Support.V4.App.Fragment
      {
          private List&lt;Model.Photo&gt; photos;
  
          private ListView listview;
          private MyListViewAdapter adapter;
          private bool IsLoading = false;
          private int Treshhold = 10;
          private int PreviousTotalItemCount = 0;
  
          public override void OnCreate(Bundle savedInstanceState)
          {
              base.OnCreate(savedInstanceState);
  
          }
  
          public override void OnResume()
          {
              base.OnResume();
  
              try
              {
                  var task = Task.Run(async delegate
                  {
                      photos = await API.GetLatestPhotos();
                  }).ContinueWith(delegate
                  {
                      using(var h = new Handler(Looper.MainLooper))
                      {
                          h.Post(delegate
                          {
                              UpdateView(); // I cannot call this.RunOnUIThread(delegate{ //code });
                          });
                      }
                  });
  
                  task.Wait();
              }
              catch { }
          }
  
          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
          {
              photos = new List&lt;Model.Photo&gt;();
  
              View view = inflater.Inflate(Resource.Layout.HomePage, container, false);
  
              listview = view.FindViewById&lt;ListView&gt;(Resource.Id.List);
              adapter = new MyListViewAdapter(this, photos);
              listview.Adapter = adapter;
              listview.ScrollStateChanged += Listview_ScrollStateChanged;
              UpdateView();
  
  
              return view;
          }
  
          private void UpdateView()
          {
              if(adapter != null)
                  adapter.NotifyDataSetChanged();
          }
  
          private async void Listview_ScrollStateChanged(object sender, AbsListView.ScrollStateChangedEventArgs e)
          {
              int last = listview.LastVisiblePosition;
              int total = listview.Adapter.Count;
  
              if(IsLoading &amp;&amp; total &gt; PreviousTotalItemCount)
              {
                  IsLoading = false;
                  PreviousTotalItemCount = total;
              }
  
              if((last + Treshhold) &gt;= total &amp;&amp; !IsLoading)
              {
                  IsLoading = true;
  
                  var items = await API.GetLatestPhotos(API.LastPhotoID);
  
  
                  foreach(var item in items)
                      photos.Add(item);
  
                  adapter.NotifyDataSetChanged();
              }
          }
      }
  
      public class MyListViewAdapter : BaseAdapter&lt;Model.Photo&gt;
      {
          List&lt;Model.Photo&gt; items;
          Activity context;
  
          public MyListViewAdapter(Android.Support.V4.App.Fragment context, List&lt;Model.Photo&gt; items) : base()
          {
              this.context = context.Activity;
              this.items = items;
          }
  
          public override long GetItemId(int position)
          {
              return position;
          }
          public override Model.Photo this[int position]
          {
              get { return items[position]; }
          }
          public override int Count
          {
              get { return items.Count; }
          }
          public override View GetView(int position, View convertView, ViewGroup parent)
          {
              var item = items[position];
              View view = convertView;
              if(view == null) // no view to re-use, create new
                  view = context.LayoutInflater.Inflate(Resource.Layout.MyListView, null);
  
              Koush.UrlImageViewHelper.SetUrlDrawable(view.FindViewById&lt;ImageView&gt;(Resource.Id.Image), item.StoragePath);
              return view;
          }
      }
  }
  </code></pre>
  
  <p>All data uploading, it's OK, but my listview doesn't update.</p>
  
  <p>If I'm trying to call <code>adapter.NotifySetDataChanged()</code> not in UI thread, I've got <code>Skippen *n* frames. Too much work in current thread.</code></p>
  
  <p>Any ideas how to fix it?</p>