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="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context="com.example.inputdialog.MainActivity" >  
  
<ListView
   android:id="@+id/listview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   />

</LinearLayout>


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"?>

<RelativeLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="5dp" >
  
<TextView
     android:id="@+id/txt"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_marginRight="10dp"
       />
  
</RelativeLayout>


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.app.Activity;
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) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     ListView list=(ListView)findViewById(R.id.listview);
     initList();
     adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txt,dataItems);
    
     list.setOnScrollListener(new OnScrollListener(){
     @Override
     public void onScrollStateChanged(AbsListView view, int scrollState) {
     // Ignore this method
     }
    
     @Override
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
     Log.i("Main",totalItemCount+"");
    
     int lastIndexInScreen = visibleItemCount + firstVisibleItem;

     if (lastIndexInScreen>= totalItemCount && !isLoading) {
     // It is time to load more items
     isLoading = true;
     loadMore();
       }
     }
     });
    
    
      list.setAdapter(adapter);
    
   }
  
   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  
   next=i;
   }
  
   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
   adapter.notifyDataSetChanged();
   isLoading=false;
   // Update next
   next=i;
   }
  
   }
    
  
}

Android-ListView-load-more-items

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.