Load more on Scroll, functionality in ListView not working android

ArvindrajaArvindraja INMember ✭✭✭
edited November 2017 in Xamarin.Android

Hi,
I am developing Listview with Load More functionality on Listview Scroll. On scroll I am hitting my API's properly getting data as well but i am not able to append new records in Listview by calling Notifydatasetchanged().
see the code i have tried to get this functionality.

This is async class to call code in background

  public class MyTask : AsyncTask
        {
            Activity _con;
            ListView _lv;
            ProgressDialog pbr;
            int _pageCount = 0;
            List<IIsSection> _items= new List<IIsSection>();
            HomeworkAdapter _homeworkAdapter;
            public MyTask(Activity con,ListView lv)
            {
                _con = con;
                _lv = lv;
            }
            protected override void OnPreExecute()
            {
                pbr = Utilities.ProgressBar(_con, "b");
                pbr.Show();
            }
            protected  override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
            {
                _pageCount += 1;
                var sid = SISConst.Mychildren.FirstOrDefault().StudentId;
                var objHomework = new HomeworkDAL();
                var item = objHomework.GetHomeWorksForStudentPages2(_pageCount.ToString(), "10", sid.ToString());  //10 is records count
                if (item !=null)
                {                                    
                     SISConst.items.AddRange(_items);
                    _homeworkAdapter = new HomeworkAdapter(_con, SISConst.items.Distinct().ToList());
                    _homeworkAdapter.NotifyDataSetChanged();
                }
                return null;
            }
            protected override void OnPostExecute(Java.Lang.Object result)
            {
                pbr.Hide();
            }
      }

This is scroll events

      public void OnScrollStateChanged(AbsListView view, [GeneratedEnum] ScrollState scrollState)
       {
                    if (scrollState == ScrollState.TouchScroll)
                    {
                        userScrolled = true;
                    }
      }   

     public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
            {
                // Now check if userScrolled is true and also check if
                // the item is end then update list view and set
                // userScrolled to false

                if (userScrolled && firstVisibleItem + visibleItemCount == totalItemCount)
                {
                    userScrolled = false;
                   var task = new MyTask(Activity,_mListView);
                    task.Execute();
                    _homeworkAdapter = new HomeworkAdapter(Activity, SISConst.items.Distinct().ToList());
                    _homeworkAdapter.NotifyDataSetChanged();             
                }
            }

This is oncreate view

  public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
      _mListView.SetOnScrollListener(this);
                     SISConst.items = items;
                    _homeworkAdapter = new HomeworkAdapter(Activity, SISConst.items.Distinct().ToList());  //items.Distinct()
                    _mListView.Adapter = _homeworkAdapter;        
   }       

Where i an making mistake i am not able to figure out
is MyTaks class itself wrong?

Thank you.

Sign In or Register to comment.