How to know Load More completed (All data loaded) in Listview android

ArvindrajaArvindraja INMember ✭✭✭

Hi,
I have developed load more functionality in listview and working fine, only issue I am facing that not able to know all data loading completed or data finished consequently my api's hitting(calling api unnecessary) on scroll after completing all data loading.
Please suggest me how to sort out this issue
Thank you.

Best Answer

  • ArvindrajaArvindraja IN ✭✭✭
    edited November 2017 Accepted Answer

    @Sreeee
    Yeah sure.

     public class HomeworkFragment :AbsListView.IOnScrollListener
      {
            public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
                {
    
                }
                public void OnScrollStateChanged(AbsListView view, [GeneratedEnum] ScrollState scrollState)
                {
                    int threshold = 1;
                    int count = _mListView.Count;
    
                    if (scrollState == ScrollState.Idle && SISConst.IsHomeworkLoaded==false)
                    {
                       if (_mListView.LastVisiblePosition >= count - threshold)
                        {
                            _pageCount += 1;
    
    
                                var task = new LoadBackgroundTask(Activity, _pageCount);
                                task.Execute();                      
                                    _homeworkAdapter.AddAll(SISConst.Items);
                        }
                    }
                }
     }
    

    this is separate class

        public class LoadBackgroundTask : AsyncTask
        {
                private Activity _con;
                private ProgressDialog pbr;
                int _pageCount;
                private List<IIsSection> _items;       
                public LoadBackgroundTask(Activity con, int pageCount)
                {
                    _con = con;
                    _pageCount = pageCount;
                }
                protected override void OnPreExecute()
                {
                    pbr = Utilities.ProgressBar(_con, "b");
                    pbr.Show();
                }
                protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
                {
                    try
                    {
                        List<StudentHomework> result;
                        _items = new List<IIsSection>();
                        var sid = SISConst.Mychildren.FirstOrDefault().StudentId;
                        var objHomework = new HomeworkDAL();
    
                            result = objHomework.GetHomeWorksForStudentPagesSync(_pageCount.ToString(), "10", sid.ToString());
    
                        if (result != null && result.Count > 0)
                        {               
                            SISConst.Items = _items;
                        }
                        else
                        {
                            if (result!=null)
                              //this line is the answer of the current thread question
                            SISConst.IsHomeworkLoaded = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        var log = LogService.CreateInstance();
                        log.MobileLog(ex, SisFragment.UserName);
                    }
                    return null;
             }
     }
    

    Addall method available in Adapter class

      public void AddAll(List<IIsSection> scrollitems)
            {
                if (scrollitems.Count>0)
                {                
                    if (_items != null)
                    {                   
                        _items.AddRange(scrollitems);
                        scrollitems.Clear();
                        SISConst.Items.Clear();
                        NotifyDataSetChanged();
                    }
                }
            }
    

    and the variables you are not understanding those are declared top globally.
    Now if my api is not returning data or stopped returning data i am making
    SISConst.IsHomeworkLoaded = true; (SISConst is public static class globally access by app ).

    if you wont understand anything just ask.

Answers

  • SreeeeSreeee INMember ✭✭✭✭

    Me too have this same issue :)

    I am using a button for loading more items, are you using button or automatical loading when end of the list reached?

  • ArvindrajaArvindraja INMember ✭✭✭

    @Sreeee
    I am not using button just on scroll it appending new items on list. However i have solved this issue

  • DarshanJSDarshanJS USMember ✭✭✭

    You can use activity indicator to know so that till will run until the data is loaded fully

  • SreeeeSreeee INMember ✭✭✭✭

    Hi @Arvindraja
    I want to implement the load more feature without the button. I try some codes for detecting the listview end and call the web service if listview end is reached, but not get lucky.

    My code:

                           ObservableCollection<string> Items;
                            var listview = new ListView();
                            listview.ItemsSource = userTweetResponse.userTweetsList;
                            Items = new ObservableCollection<string>();
                            listview.ItemAppearing += (sender, e) =>
                            {
                                    //hit bottom!
                                    if (e.Item.ToString() == Items[Items.Count - 1])
                                    {
                                        loadMoreButton.IsVisible = true;
                                    }
                            };
    

    In xaml I make the button visibility false and if list hit the bottom make the button visible true.... But button not showing in the UI after reaching the end of list....

    Do you have any suggestion?

    Can you share the code for detecting the end of listview and stop the web service call if all items are loaded?

    Thanks in advance....

  • ArvindrajaArvindraja INMember ✭✭✭

    @DarshanJS thank you.
    If think you are not understood my question. I am asking when listview loaded full data, it should not hit API's.
    Anyhow I am running/showing progressbar to get next page records.

  • ArvindrajaArvindraja INMember ✭✭✭
    edited November 2017 Accepted Answer

    @Sreeee
    Yeah sure.

     public class HomeworkFragment :AbsListView.IOnScrollListener
      {
            public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
                {
    
                }
                public void OnScrollStateChanged(AbsListView view, [GeneratedEnum] ScrollState scrollState)
                {
                    int threshold = 1;
                    int count = _mListView.Count;
    
                    if (scrollState == ScrollState.Idle && SISConst.IsHomeworkLoaded==false)
                    {
                       if (_mListView.LastVisiblePosition >= count - threshold)
                        {
                            _pageCount += 1;
    
    
                                var task = new LoadBackgroundTask(Activity, _pageCount);
                                task.Execute();                      
                                    _homeworkAdapter.AddAll(SISConst.Items);
                        }
                    }
                }
     }
    

    this is separate class

        public class LoadBackgroundTask : AsyncTask
        {
                private Activity _con;
                private ProgressDialog pbr;
                int _pageCount;
                private List<IIsSection> _items;       
                public LoadBackgroundTask(Activity con, int pageCount)
                {
                    _con = con;
                    _pageCount = pageCount;
                }
                protected override void OnPreExecute()
                {
                    pbr = Utilities.ProgressBar(_con, "b");
                    pbr.Show();
                }
                protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
                {
                    try
                    {
                        List<StudentHomework> result;
                        _items = new List<IIsSection>();
                        var sid = SISConst.Mychildren.FirstOrDefault().StudentId;
                        var objHomework = new HomeworkDAL();
    
                            result = objHomework.GetHomeWorksForStudentPagesSync(_pageCount.ToString(), "10", sid.ToString());
    
                        if (result != null && result.Count > 0)
                        {               
                            SISConst.Items = _items;
                        }
                        else
                        {
                            if (result!=null)
                              //this line is the answer of the current thread question
                            SISConst.IsHomeworkLoaded = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        var log = LogService.CreateInstance();
                        log.MobileLog(ex, SisFragment.UserName);
                    }
                    return null;
             }
     }
    

    Addall method available in Adapter class

      public void AddAll(List<IIsSection> scrollitems)
            {
                if (scrollitems.Count>0)
                {                
                    if (_items != null)
                    {                   
                        _items.AddRange(scrollitems);
                        scrollitems.Clear();
                        SISConst.Items.Clear();
                        NotifyDataSetChanged();
                    }
                }
            }
    

    and the variables you are not understanding those are declared top globally.
    Now if my api is not returning data or stopped returning data i am making
    SISConst.IsHomeworkLoaded = true; (SISConst is public static class globally access by app ).

    if you wont understand anything just ask.

  • SreeeeSreeee INMember ✭✭✭✭

    @Arvindraja Thanks for your response. I think your implementation is different from mine.

    In my project, initially I am loading 20 items in the UI. when the end of the list reached, I am showing a button. On click of that button, a web service call is started for fetching the items from 1 to 40 not from 21 to 40 (adding 20 items to ui on button click). Again onclick of button load the items from 1 to 60.
    My code :

    var response = await client.GetAsync("My url");
    string tweetJson = await response.Content.ReadAsStringAsync();
    UserTweetResponse userTweetResponse = new UserTweetResponse();
                  if (tweetJson != "")
                    {
                        userTweetResponse = JsonConvert.DeserializeObject<UserTweetResponse>(tweetJson.ToString());
                    }
     ListView1.ItemsSource = userTweetResponse.userTweetsList;
    

    UserTweetResponse is my model class.
    In my xaml I am using {Binding} property to show the data in UI.
    Inside the myurl I am adjusting the count values.

    So in this implementation how can I know if the list reaches the bottom?

    Is my shared codes are enough to give a solution? :)

    Thanks in advance....

  • ArvindrajaArvindraja INMember ✭✭✭
    edited November 2017

    Exaclty same I am doing but accessing 10 records.
    //this is web api calling
    result = objHomework.GetHomeWorksForStudentPagesSync(_pageCount.ToString(), "10", sid.ToString());

    List reaches to bottom for knowing that need to override below method

      public void OnScrollStateChanged(AbsListView view, [GeneratedEnum] ScrollState scrollState)
       {
    
             if (scrollState == ScrollState.Idle)
                        {
                           if (_mListView.LastVisiblePosition >= count -1)
                            {
    
                              //this is calling when you hit bottom of list
                                _pageCount += 1;
    
                            }
          }
    

    Don't implement it in adapter class. for that you need to override OnScrollStateChanged method.

  • SreeeeSreeee INMember ✭✭✭✭

    Hi @Arvindraja: How can I access OnScrollStateChanged()?
    In my project showing errors for OnScrollStateChanged(). For accessing this method, what I need to do first?

  • ArvindrajaArvindraja INMember ✭✭✭
    edited December 2017

    For accessing OnScrollStateChanged() method you need to extend your activity/Fragment with AbsListView.IOnScrollListener interface as i already posted code before. have a look on that
    Thank you.

  • SreeeeSreeee INMember ✭✭✭✭

    Thanks for your response :)
    I am extending to a contentpage not an activity or fragment. My class already extend contentpage like below:

    public partial class DashBoardPage : ContentPage
    

    I remove the contentpage and give AbsListView.IOnScrollListener but getting AbsListView not found error :'(

  • ArvindrajaArvindraja INMember ✭✭✭

    @Sreeee
    You are using XamarinForms, for this lot many tutorials available for the same task, go through those.

  • CharwakaCharwaka INMember ✭✭✭✭

    @Sreeee said:
    Thanks for your response :)
    I am extending to a contentpage not an activity or fragment. My class already extend contentpage like below:

    public partial class DashBoardPage : ContentPage
    

    I remove the contentpage and give AbsListView.IOnScrollListener but getting AbsListView not found error :'(

    please post in Xamarin Forms Forum

  • SreeeeSreeee INMember ✭✭✭✭

    @Charwaka said:

    @Sreeee said:
    Thanks for your response :)
    I am extending to a contentpage not an activity or fragment. My class already extend contentpage like below:

    public partial class DashBoardPage : ContentPage
    

    I remove the contentpage and give AbsListView.IOnScrollListener but getting AbsListView not found error :'(

    please post in Xamarin Forms Forum

    https://forums.xamarin.com/discussion/111449/load-more-items-without-button-click/p1?new=1

Sign In or Register to comment.