3

I want to get new data and update it into my listView using the code below, What i want is when i scroll until the bottom of the listView, it will automatically update and add new data into the list.

E.g: like facebook, only add new data to the bottom without scroll and keep the current position

But i was end up to having some problem. Which are

1) the listview is updated,but it scrolled back to the top.

listview .setOnScrollListener(new OnScrollListener() {
    private int currentVisibleItemCount;
    private int currentScrollState;
    private int currentFirstVisibleItem;
    private int totalItem;

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        this.currentScrollState = scrollState;
        this.isScrollCompleted();
    }

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

        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
        this.totalItem = totalItemCount;

    }
    private void isScrollCompleted() {

        if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                && this.currentScrollState == SCROLL_STATE_IDLE) {

            Log.d("this is", "the end");
            page = page + 1;
            call_function(page);

        }

    }
});

....
....
public void call_function(int page_number) {
...
...
    ListAdapter adapter = new SimpleAdapter(
                            getActivity(), user_List,
                            R.layout.user, new String[] {"user","date"},
                            new int[] {R.id.usr_name ,R.id.register_time});

    // updating listview

    setListAdapter(adapter);
}

any solution to handle this exceptions??

thanks in advance.

initramfs
  • 8,275
  • 2
  • 36
  • 58
enzo
  • 309
  • 3
  • 15

5 Answers5

5

When you update your Adapter's data you can save the list current selected item index and set it back when the data is rendered. Something like this:

int index = mList.getFirstVisiblePosition();
// do your update stuff
mList.smoothScrollToPosition(index);
Kiril Aleksandrov
  • 2,601
  • 20
  • 27
2

There is a simple solution. Do not add a new ListAdapter every time you update your values, instead update current. Once you poped new data in your adapter, call

adapter.notifyDataSetChanged();
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
2

It is best to load the adapter in async and in pre execute keep the current position using int currentIndex=displaylist.getFirstVisiblePosition(); and in postExecute after notifying the adapter call displaylist.smoothScrollToPosition(currentIndex); your currently viewing position will be kept steady after refresh.

Koustuv Ganguly
  • 897
  • 7
  • 21
0

You need to retain list view scroll position, solution for this is already explained here Reataining listview scroll position

Community
  • 1
  • 1
Narendra
  • 609
  • 1
  • 12
  • 28
0

May i know how to change the following adapter into an adapter that support notifydatachanged???

ListAdapter adapter = new SimpleAdapter(
                            getActivity(), feeds_List,
                            R.layout.feed_item, new String[] {"username","datetime","pts",},
                            new int[] {R.id.usr_name ,R.id.feed_time,R.id.no_stamp});
setListAdapter(adapter);

feed_list is the ArrayList<HashMap<String, String>>that the data updated to.

R.id.usr_name ,R.id.feed_time,R.id.no_stamp

is the layout objects in the listview.

all the code are inside a listactivity.

enzo
  • 309
  • 3
  • 15