0

Initially the listView has 20 items. I need to update the listView with next 20 items when user scrolls down all the way to the bottom. I am using ArrayAdapter to achieve this. The items in the listView are getting from the REST API which takes startIndex and the limit as parameter using EventBus(not AsyncTask).

    public void onEventMainThread(MessageListEvent event) {
    //Create Adapter
    //Set Adapter to List View
    customListAdapter = new CustomListAdapter(getActivity(), event.itemList);

    Log.d("Scroll","Inside OnEventMainThread");
    mListView = (ListView) view.findViewById(android.R.id.list);

    mListView.setAdapter(customListAdapter);
    mListView.setOnItemClickListener(this);

    // This loads the next 20 images when scrolled all the way down to bottom.
    // Overrides onScroll and onScrollStateChanged function
    mListView.setOnScrollListener(this);
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    int loaded = firstVisibleItem+visibleItemCount;
    if(loaded>=totalItemCount && startIndex+19==loaded) {
        startIndex = loaded+1; //Static variable used in onEventBackgroundThread(...). Download starts from this item
        EventBus.getDefault().post(new StartDownloadEvent()); // This will download the next 20 and update the list to 40 items but position starts from 0 again. onEventBackgroundThread(...) downloads the data and updates the list.
    }
}

I found similar problem in stackOverflow but all are using AsyncTask. I don't want to use AsyncTask and would like to achieve using EventBus.

1 Answers1

0

AsyncTask and EventBus are two totally different things. AsyncTask is used to do stuff off the main thread, things that might take a long time (like network stuff) and would block the main thread if not done on another thread. EventBus is just a means of communication between the parts of your app (Activities, Fragments etc).

While you sure can use EventBus to inform some class that more items need to be loaded for your list and you also can use it to inform your ListView/ adapter that new items have been loaded, it will not do any network stuff like loading new items for you. You'll either have to do that on your own (off the main thread, e.g. AsyncTask) or use some library like this one.

When you're doing
EventBus.getDefault().post(new StartDownloadEvent()); nothing will happen except you have a class that receives the event and does the api call.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • I am new to Android and this helped me to understand much better. As u told i am able to inform adapter with the new list. i.e. the new list contains the existing 20 items and the new 20 items, total 40 items. But i am loosing the position when i scroll down to 20th item. The list reloads with 40 items starting from 0. – androidbeginner Mar 13 '15 at 18:11
  • You obviously create a new adapter when receiving the new items(on MessageListEvent) which you don't have to, just call adapter.notifyDataSetChanged. If that isn't the case you'll probably have to ask a new question and post your updated code. – fweigl Mar 13 '15 at 18:16
  • I am adding the next 20 items to the adapter and calling notifyDataSetChanged like below. ` getActivity().runOnUiThread(new Runnable() { @Override public void run() { customListAdapter.addAll(newItemList); customListAdapter.notifyDataSetChanged(); } });` It is still not working. I tried with `mListView.setSelection(startIndex);` but no use. But works when i write the above code outside runOnUIThread function and fails after loading 40 items. – androidbeginner Mar 31 '15 at 16:13