Android ListView load more items

When you have a lot of records in your database (e.g. 1000 records), it is not good at all to load all the records into a ListView at once because it is very slow. If you are facing this situation, the best option is to implement pagination to the ListView. You can load ten items at first. Then you load more items when the ListView scrolls to the last item. To achieve this goal, you need to implement the OnScrollListener interface. You will override the onScroll() method to check whether the ListView is scrolled to the last item. The onScroll() method has thee parameters that help you do the task. These parameters are firstVisibleItem, visibleItemCount, and totalItemCount. If the sum of firstVisibleItem and visibleItemCount is greater than or equal to the totalItemCount, it indicates that the ListView is scrolled to the last item. Then you can load more items. In the example application below, initially, the ListView loads ten items. Then it is scrolled to the last item, more ten items are loaded until the number of items reach one hundred.

Now to have an example application on loading more items when the ListView is scrolled to the last item, you create a new Android project. Then modify the activity_main.xml file to add a ListView.

<LinearLayout xmlns:android=""
   tools:context="com.example.inputdialog.MainActivity" >  


Then in the res/layout folder, you create a layout file (list_item.xml) of the ListView as shown below.

<?xml version="1.0" encoding="utf-8"?>

   android:padding="5dp" >

In onCreate() method of the MainActivity class, you write code to add ten items to the ArrayList, create an ArrayAdapter instance, set the instance to the ListView, and override the onScroll() method to load more items when the list is scrolled to the end.

import java.util.ArrayList;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity{

ArrayAdapter<String> adapter;
ArrayList<String> dataItems;
boolean isLoading=false;
int next;
   protected void onCreate(Bundle savedInstanceState) {
     ListView list=(ListView)findViewById(;
     adapter=new ArrayAdapter<String>(this,R.layout.list_item,,dataItems);
     list.setOnScrollListener(new OnScrollListener(){
     public void onScrollStateChanged(AbsListView view, int scrollState) {
     // Ignore this method
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
     int lastIndexInScreen = visibleItemCount + firstVisibleItem;

     if (lastIndexInScreen>= totalItemCount && !isLoading) {
     // It is time to load more items
     isLoading = true;
   public void initList(){
   // Create ArrayList object
   dataItems=new ArrayList<String>();
   // Add ten items to the list
   int i;
   for(i=1;i<=10;i++) dataItems.add("Item"+i);
   // Record beginning index to be added to list  
   public void loadMore(){
   int i;
   if(dataItems.size()<=90){ // Limit the number of items to 100 (stop loading when reaching 100 items)
   for(i=next;i<=next+9;i++) dataItems.add("Item"+i);
   // Notify the ListView of data changed
   // Update next


Posted by: Dara | post date: 01-03-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.