onScroll implementation for infinite scroll
<p>The expected output is that data should be loaded once and then for every subsequent scroll. But instead, it is loading all the data in the listview at once.</p>

<pre><code>public class ViewPage extends ListActivity{

ListView lv;
ListAdapter ld;
int size=3;

ArrayList&lt;String&gt; filename = new ArrayList&lt;String&gt;();
Cursor contactcursor;
ArrayAdapter&lt;String&gt; arr_ad;
int contact_column_index;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mylist);




    String[] proj = { ContactsContract.Contacts.DISPLAY_NAME };
    contactcursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, proj, null, null, null);
    contact_column_index = contactcursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
    lv = (ListView)findViewById(android.R.id.list);
    contactcursor.moveToFirst();
        for(int i=0;i&lt;size;i++)
        {

            filename.add(contactcursor.getString(contact_column_index));
            if(!contactcursor.moveToNext())
                break;
        }

        arr_ad = new ArrayAdapter&lt;String&gt;(ViewPage.this, android.R.layout.simple_list_item_1, filename);
        setListAdapter(arr_ad);


        lv.setOnScrollListener(new EndlessScrollListener());
}

public class EndlessScrollListener implements OnScrollListener {

private int visibleThreshold = size;
private int currentPage = 0;
private int previousTotal = 0;
private boolean loading = true;

public EndlessScrollListener() 
{

}

public EndlessScrollListener(int visibleThreshold) 
{
    this.visibleThreshold = visibleThreshold;
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub



    /*Runnable action = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }
    };
    runOnUiThread(action);*/

    Toast.makeText(ViewPage.this, "In" , 1).show();
     if (loading) 
     {
         if (totalItemCount &gt; previousTotal) 
         {
            loading = false;
            previousTotal = totalItemCount;
            currentPage++;
         }
     }
     if (!loading &amp;&amp; (totalItemCount - visibleItemCount) &lt;= (firstVisibleItem + visibleThreshold)) 
     { 


            for(int i=0;i&lt;size;i++)
            {

                if(!contactcursor.moveToNext())
                    break;


                filename.add(contactcursor.getString(contact_column_index));

            }

            arr_ad.notifyDataSetChanged();

            loading = true;
            Toast.makeText(ViewPage.this,totalItemCount+" "+visibleItemCount+" "+firstVisibleItem+" "+visibleThreshold+" ", 1).show();
     }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub

}

}
}
</code></pre>

<p>The onscroll method in the endlessscrolllistener class runs and loads the complete data without even the scrolling being done. Thanx in advance.</p>
