Dynamically adding items to a multi column listView on scroll
<p>I have a multi-column listView and I would like to dynamically add 10 items each to the list on the scroll event. Dynamically adding the lit items have been successfull but the listview selection goes back to the first item of the list when the next 10 items are added.</p>

<p>Here is my code : </p>

<pre><code>public class ViewCheckInCheckOutHistory extends Activity {
        ListView historyListView;

        ProgressDialog pd;
        List&lt;CheckInCheckOutHistory&gt; checkInCheckOutHistoryList = new ArrayList&lt;CheckInCheckOutHistory&gt;();    

        ArrayList&lt;HashMap&lt;String, String&gt;&gt; historyArrayList;
        SimpleAdapter histroyListAdapter;

    int itemsPerPage = 10;
        boolean loadingMore = false;

        int firstItemCount = 0;
        int lastItemCount = 10;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.view_checkin_checkout_history);     

        pd = ProgressDialog.show(this, "", "Please wait...");

        Thread thread = new Thread() {

            public void run() {

            synchronized (this) {
                fetchHistory();

                handler.post(new Runnable() {
                public void run() {
                    pd.dismiss();

                    displayUI();
                };
                });
            }
            }
        };

        thread.start();
        }

    private void displayUI() {  
        if ((checkInCheckOutHistoryList != null)
            &amp;&amp; (checkInCheckOutHistoryList.size() &gt; 0)) {
        historyArrayList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();

        histroyListAdapter = new SimpleAdapter(ViewCheckInCheckOutHistory.this,
            historyArrayList,
            R.layout.multi_colummn_list_text_style_small,
            new String[] { "assetTag", "action", "actionTime" }, new int[] {
            R.id.list_content_column1,
            R.id.list_content_column3,
            R.id.list_content_column4});

        historyListView.setOnScrollListener(new OnScrollListener(){

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

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

            int lastInScreen = firstVisibleItem + visibleItemCount;             

            if((lastInScreen == totalItemCount) &amp;&amp; !(loadingMore)){                 
                Thread thread =  new Thread(null, loadMoreListItems);
                thread.start();
            }               
            }
        });
        }
        }


        //Runnable to load the items 
        private Runnable loadMoreListItems = new Runnable() {

        @Override
        public void run() {
            //Set flag so we cant load new items 2 at the same time
            loadingMore = true;

            HashMap&lt;String, String&gt; historyObjectMap;

            System.out.println("firstItemCount : "+firstItemCount);
            System.out.println("lastItemCount : "+lastItemCount);

            //Get 10 new listitems
            for (int i = firstItemCount; i &lt; lastItemCount; i++) {
            System.out.println("i : "+i);
            CheckInCheckOutHistory checkOutHistoryObj = checkInCheckOutHistoryList.get(i);

            historyObjectMap = new HashMap&lt;String, String&gt;();
            historyObjectMap.put("assetTag", checkOutHistoryObj.getAssetTag());
            historyObjectMap.put("action", checkOutHistoryObj.getAction());

            historyArrayList.add(historyObjectMap);             
            }

            runOnUiThread(returnRes);
        }
        };  

        //Since we cant update our UI from a thread this Runnable takes care of that! 
        private Runnable returnRes = new Runnable() {
        @Override
        public void run() {

            //Add the new items to the adapter
            if(historyArrayList != null &amp;&amp; historyArrayList.size() &gt; 0){            
            histroyListAdapter.notifyDataSetChanged();
            }

            if (checkInCheckOutHistoryList.size() - lastItemCount &gt; 10) {
            firstItemCount = lastItemCount;
            lastItemCount = lastItemCount + itemsPerPage;
            } else {
            if (checkInCheckOutHistoryList.size() - firstItemCount &gt; 10) {
                firstItemCount = lastItemCount;
                lastItemCount = checkInCheckOutHistoryList.size();
            } else {
                if (checkInCheckOutHistoryList.size() - firstItemCount &gt; 0){
                firstItemCount = lastItemCount;
                lastItemCount = checkInCheckOutHistoryList.size();
                } 
            }
            }

            historyListView.setAdapter(histroyListAdapter);

            //Done loading more.
            loadingMore = false;
        }
        };
    }
</code></pre>

<p>'fetchHistory()' is the function where the values are added in the 'checkInCheckOutHistoryList'.</p>

<p>Thanks in advance for any help.</p>
