Динамическое добавление элементов в список с несколькими столбцами при прокрутке

1

У меня есть список столбцов с несколькими столбцами, и я хотел бы динамически добавлять по 10 элементов в список в событии прокрутки. Динамическое добавление освещенных элементов прошло успешно, но выбор списка возвращается к первому элементу списка при добавлении следующих 10 элементов.

Вот мой код:

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()' - это функция, в которой значения добавляются в 'checkInCheckOutHistoryList'.

Заранее благодарим за помощь.

  • 0
    Список отображается с первого элемента, поскольку вы снова и снова назначаете адаптер для ListView . notifyDatasetChanged() должно быть достаточно. С другой стороны, использование AsyncTask было бы лучше, чем явное создание потоков с помощью Runnables .
Теги:

1 ответ

3
Лучший ответ

В методе displayUI() вы каждый раз создаете новый адаптер и устанавливаете его в listView вместо Adapter.notifyDataSetChanged()

  • 0
    Я попытался, удалив historyListView.setAdapter (histroyListAdapter); из Runnable returnRes. Но это не сработало.
  • 0
    Не могли бы вы сказать, где я могу установить адаптер для listView?
Показать ещё 3 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню