Android – ListView to load more items when reached end-ThrowExceptions

Exception or error:

I have successfully implemented a listView where each row is an object I’ve created.
I have an interface which reads some objects and returns an array of them, which I pass to the adapter constructor.
What I want to do is, since the list may be very long, My interface allows to read each time some more items (page 0, page 1…) so I Want, when the user reaches the last row of the list, load the next page and insert it to the listView.
What is the proper way of doing so?
I actually want the “reach end of list” trigger a callback where I can request for more pages and load them to the list.
A bit OT to my main question, But I dont mind using a baseAdapter, both would work, I just dont know what’s better for the task.

How to solve:

You can add a footer on the listView which will have a button named LoadMore. Here is a complete tutorial ListView with Load More Button
Or you can implement onscrolllistener() and add this listener to your ListView

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
 }

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work for load more date! ***/
        if(!isLoading){
             isLoading = true;
             loadMoreData();
        }
    }
}

Hope it will help

Leave a Reply

Your email address will not be published. Required fields are marked *