Kannan Suresh
Kannan Suresh

Reputation: 4580

Dynamically adding items to a multi column listView on scroll

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.

Here is my code :

public class ViewCheckInCheckOutHistory extends Activity {
        ListView historyListView;

        ProgressDialog pd;
        List<CheckInCheckOutHistory> checkInCheckOutHistoryList = new ArrayList<CheckInCheckOutHistory>();    

        ArrayList<HashMap<String, String>> 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)
            && (checkInCheckOutHistoryList.size() > 0)) {
        historyArrayList = new ArrayList<HashMap<String, String>>();

        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) && !(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<String, String> historyObjectMap;

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

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

            historyObjectMap = new HashMap<String, String>();
            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 && historyArrayList.size() > 0){            
            histroyListAdapter.notifyDataSetChanged();
            }

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

            historyListView.setAdapter(histroyListAdapter);

            //Done loading more.
            loadingMore = false;
        }
        };
    }

'fetchHistory()' is the function where the values are added in the 'checkInCheckOutHistoryList'.

Thanks in advance for any help.

Upvotes: 1

Views: 1627

Answers (1)

ingsaurabh
ingsaurabh

Reputation: 15267

In displayUI() method you are creating a new adapter every time and setting it to the listView instead use Adapter.notifyDataSetChanged()

Upvotes: 2

Related Questions