Loading data in listview on scroll without refreshing list

L

This tutorial will give you brief idea about how to load the data in ListView on scroll down.

i came across this situation when i was developing the app where only 10 records to be displayed at start. On scroll the list when user reaches the last record more data gets added to the list like facebook posts.

The idea behind this was to get 10 records from web service first and display the list.

Using the method

public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)

of OnScrollListener Check whether the scroll reached to the end

start background task to fill further data and on completion call notifyDatasetChanged() method of List adapter

package com.nanostuffs.antique; 

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

public class Main extends ListActivity implements OnScrollListener {

List<ListItems> list;
public ProgressDialog dialog;
String url=””;
int page =1;
ListView lv;
AListAdapter listAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
dialog = new ProgressDialog(this);
url =”http://yourwebserviceurl.com + page=”+page;
/**
* fill the list randomly for first view
*/
try{
new PerformBackgroundTask().execute(“”);
}catch (Exception e) {
if(dialog.isShowing())
dialog.dismiss();
e.printStackTrace();
Toast.makeText(getBaseContext(),”No Result Found”, Toast.LENGTH_LONG).show();
}
lv = getListView();
lv.setOnScrollListener(this);
}
/**
* load data using background task
*/
class PerformBackgroundTask extends AsyncTask<String, Void, String> {
List<ListItems> list1 ;
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

//check if we got the data
if(list1==null){
if (dialog.isShowing()) {
dialog.cancel();
}
}else{
if(list!=null){
// instanciate the adapter once
if(listAdapter== null){
listAdapter = new AListAdapter(Main.this, (ArrayList<ListItems>) list);
setListAdapter(listAdapter);
}
//
listAdapter.notifyDataSetChanged();
}
if (dialog.isShowing())
dialog.cancel();
}
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setMessage(“PleaseĀ  wait…”);
dialog.show();
}
@Override
protected String doInBackground(String… params) {
// TODO Auto-generated method stub
try{

// get the data from web service
list1 = new XMLReader().getDetailItemList(url);

//check if previous data availableĀ  then add new data to the same arraylist
if(list!=null){
list.addAll(list1);
}else
{
list =list1;
}
}catch (Exception e) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
return “”;
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if(totalItemCount>10){
boolean loadMore = /* maybe add a padding */
firstVisibleItem + visibleItemCount >= totalItemCount-1;

Log.d(“onScroll:”,”loadMore f=” + firstVisibleItem + “, vc=” + visibleItemCount + “, tc=” +totalItemCount);
if(loadMore) {
page++;
new PerformBackgroundTask().execute(“”);
}else{
Log.d(“recrd”, “NO”);
}
}
}
//Debug.stopMethodTracing();
}

public int checkDataSize() {
// check if the data available
/**
* do your loading of data
*/
return result.length();

}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

}

 

About the author

mahesh.muley
By mahesh.muley

Category