0

So, I have a listview and getting the data for it from an external database. I would like to have 20 items the first time, then if the user scrolls down it loads another 20 and so on.

class ItemAdapter extends BaseAdapter {
        private ArrayList<Item> objects;
        private class ViewHolder {
             public TextView text_tt;  

        }

        @Override
        public int getCount() {
            return SIZE;
            //return 9;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        } 

@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
            final ViewHolder viewHolder;
            if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.text_tt = (TextView) v.findViewById(R.id.toptext);
            } else {
            viewHolder = (ViewHolder) convertView.getTag();
            }

            if(position==getCount()-1){
                LoadMore(); //asynctask - load other 20

            }
            return v;
        }
    }

In the load function I parse the pre-read json data so i just have to add another 20 one to the list and notifyDataSetChanged().. The function works well but it has a side effect: about 50% of the time the click on items is not recognized. I scroll down, receive the next 20 but I cannot click on any items. But for example if I change activity and come back to the listview, it works. Why? Thank you!

Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • Dude u should be using listview methods to detect when to load more items. Add scroll listener for listview. Check when it has reached last scroll position and then call LoadMore(). Dont call LoadMore in getView. I think should read about flyweight pattern of listview. you are thinking that for each data a new item is created for your listview. The above is definitely a troublesome code – Pulkit Sethi Jul 29 '13 at 00:56
  • Thanks for the advice, i think vikram just showed me an example which works so far. I saw my method here on stackoverflow but i didnt know this was a bad approach. – Jani Bela Jul 29 '13 at 10:18

1 Answers1

2

You can set a OnScrollListener on your ListView. In the onScroll() method, check if:

firstVisible + visibleItemCount = totalItemCount

If this condition is satisfied, you can load more items from the database, reinitialize the adapter with the updated list of items.

listView.setOnScrollListener(new OnScrollListener() {

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

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

                // add 20 more items to the list and reinitialize the adapter

            } 
        }
    });
}
Vikram
  • 51,313
  • 11
  • 93
  • 122