firstVisibleItem passed to onScrollListener does not update when more items are added to the list in listView

advertisements

I have a list view which gets its contents from the web. on my onscrollListener, I have a check, so that if the firstVisibleItem is less than a thresh hold, I download more data. I also have checks to make sure I don't try to get more items when a previous get is still in progress. But even after the get data request has completed, new data has been added to the data list, and notifyDataSetChange has been called, my firstVisibleItem is not updated (ie still shows the previus value that triggered the request for more data, even though the list has grown and the same row should now have a bigger number as its position). So in essence, once I hit the threshold for getting more data the first time, it will start a chain of events that will keep downloading more and more data until all rows have been retrieved. I am using a LinkedList for my data. I need to be able to add items on both ends of my list.

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

            if (moreMessages && !getMessagesInProgress && (firstVisibleItem < 10)) {

                getMessages(NUM_MESSAGES_TO_GET, "after");
                getMessagesInProgress = true;
            }
}

When I successfully get the next chunk of data from the server, I add them it to my linked list using LinkedList.addFirst() in a for loop, call notifyDataSetChanged, and set getMessagesInProgress to false. My ListView is stacked from bottom.

UPDATE: When I say my firstVisibleItem does not update, I'm NOT talking about the view on the screen, I mean that the next time my OnScrollListener() gets called, the value passed in as firstVisibleItem is still the same as before, even though I have added 20 more downloaded items to the end of the list. so the row which was row position 1, should now be 21. (my list is stalked from bottom, its a message thread, so user starts at bottom of list and swipes down to see older messages which are at the top of the list. When firstVisibleItem < 10, I download 20 older messages and add to my linked list, so now the position of all the previous items should be +20. This doesnt happen and as a result, the first time firstVisibleItem becomes <10, my code continuously sends requests to the server for more messages)

I have not included my getView code, cuz this isn't a UI/visual issue. But here is the code from my custom BaseAadapter relating to "position"

@Override
public int getCount() {
    return messages.size();
}

@Override
public Object getItem(int position) {
    return messages.get(new Integer(position));
}

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


try in this way when you add data in list then remove or update first visible item.

and In getView() of list, you are using if condition then write their else part too