Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5930885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:36:58+00:00 2026-05-22T14:36:58+00:00

I have a multi-column listView and I would like to dynamically add 10 items

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:36:59+00:00Added an answer on May 22, 2026 at 2:36 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read this excellent Multi-column list article , as well as this question
Possible Duplicate: nullPointerException in multi column list I have following app which fetches data
I have created a multi column datastore on a table that allows me to
I have a custom, multi-column sort attached to my jqGrid instance by means of
How do I retrieve a multi-column PK in MySQL? For example I have my
I have a multi-line string that I want to do an operation on each
I have a multi column report in crystal report.Is there any way to start
I am simulating a multi-column combobox by introducing spaces between each column, such that
I'm trying to figure out how this jQuery plugin works: http://codeasily.com/jquery/multi-column-list-with-jquery In the plugin
Please note, I'm not asking how to implement or code a multi-column list. There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.