Question

I'm attempting to add items to an adapter as the user scrolls to the bottom of the list. I am able to do this, however I want to add the new items to the adapter and keep the items that were already there, but as it is now the original items are removed and only the new ones are visible. Any ideas as to how I can achieve this?

This is what my code looks like atm:

public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {

        if (loading) {      
            if (totalItemCount > previousTotal) {
                previousTotal = totalItemCount;
                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading
                && ((firstVisibleItem + visibleItemCount) >= totalItemCount)) {
            if (fragmentMode == 5) {
                ActiveUser.getInstance().FetchVidPage(currentPage, chanID,
                        activity, this);
                loading = true;
            }
        }
}

@Override
public void onTaskFinished() {
    titles.addAll(ActiveUser.getInstance().getWebVidInfo(1));
    thumbs.addAll(ActiveUser.getInstance().getWebVidInfo(2));
    data.addAll(ActiveUser.getInstance().getWebVidInfo(3));
    describtions.addAll(ActiveUser.getInstance().getWebVidInfo(4));
    vidOwnerIDs.addAll(ActiveUser.getInstance().getWebVidInfo(4));

    notifyDataSetChanged();

}

Just to clearify what I'm doing; Once the bottom of the list is visible I start an AsyncTask that fetches more videos. In the asynctask I set the adapter as a listener for an event thats fired once the asynctask is finished - this works.

onTaskFinished() the last method above - here I add the new items to 4 lists which the adapter uses to fill out each list item with, and finally I notify the adapter.

So the new items are all added fine, the problem is that the items that were in the before the new ones were added, are for some reason removed, or not visible anymore.

Forgot to add that I have verified that the length of the lists does increase, meaning it does contain both the original data and the newly added

Any ideas?

[EDIT] Adding code of where i set the adapter to the listview

        adapter = new LazyAdapter(getActivity(), ActiveUser.getInstance()
                .getWebVidInfo(3),
                ActiveUser.getInstance().getWebVidInfo(1), ActiveUser.getInstance()
                        .getWebVidInfo(2),
                ActiveUser.getInstance().getWebVidInfo(5), ActiveUser.getInstance()
                        .getWebVidInfo(4), CHAN_ID, FRAGMENT_MODE);
        listing.setAdapter(adapter);
        listing.setOnScrollListener(adapter);
Was it helpful?

Solution

Answering my own question.

Turns out the problem was my lack of understanding of what the notifyDataSetChanged() does. It invokes the adapter being recreated with the same constructor parameters as when it was initially created. So me adding the new items to the adapters internal lists had no effect whatsoever. Instead I needed to make sure that the lists that were parsed to the adapter through its constructer contained all the items.

So while my method before did get the adapter's internal lists to contain new and old data, the totalItemCount of the adapter wasn't updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top