0

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);
Line
  • 293
  • 1
  • 4
  • 13
  • This might appear to happen if your adapter isn't updating the value it returns from getCount. Is that a possibility? – Phillip Fitzsimmons Mar 20 '12 at 16:48
  • @PhillipFitzsimmons The value it gets from getCount is the length of the list called "titles", so don't think thats the issue. – Line Mar 20 '12 at 16:54
  • post code for when you set adapter to the listview , and signature of your custom adapter... – Rahul garg Mar 20 '12 at 17:36
  • @Rahulgarg added the code for when I set the adapter, not sure what you mean by signature - can you elaborate? – Line Mar 20 '12 at 17:41
  • i meant constructor...doesn't matter nw btw.... are you accessing titles,thumbs etc directly in your getView() ? your issue seems very strange...pls chek once again...: is new list containing (all values old+new)? and the view only showing **new** values not old?. – Rahul garg Mar 20 '12 at 18:06
  • @Rahulgarg yea I'm accessing the lists directly in getView().. The lists definitely contain both old and new data, but only the new data is visible. The totalItemCount in onScroll is the same before and after the new items being added, which is where the problem must be. Just don't know why.. – Line Mar 20 '12 at 18:21

1 Answers1

1

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.

Line
  • 293
  • 1
  • 4
  • 13