Load more data from database and show listview in android browser.

I want to load data when the ScrollView reaches a threshold. First, I want to show the last 20 rows from the database and show in the ListView, after using SwipeRefreshLayout, load 20 more rows from the database and show in the ListView, etc.

thank

+3


source to share


2 answers


What you are looking for is called EndlessScrollListener . You will need to extend the class EndlessScrollListener

and implement the method onLoadMore

.

public class MyEndlessScrollListener extends EndlessScrollListener {

   @Override
   public void onLoadMore(int page, int totalItemsCount) {
        loadData(page);  
   }
}

      

And set the listener to listView:



listView.setOnScrollListener(new EndlessScrollListener());

      

You can use other constructors if you want EndlessScrollListener

.

+2


source


This is what I used to load more data at the end of the list.

 listview.setOnScrollListener(new OnScrollListener(){

        @Override
        public void onScroll(AbsListView view,
        int firstVisibleItem, int visibleItemCount,
        int totalItemCount) {
        //Algorithm to check if the last item is visible or not
        final int lastItem = firstVisibleItem + visibleItemCount;
        if(lastItem == totalItemCount){                 
        // you have reached end of list, load more data probably call your database to load more data            
        }

        @Override
        public void onScrollStateChanged(AbsListView view,int scrollState) {
        //blank, not using this
        }
        });

      



Hope this link can help you

0


source







All Articles