I have a listview that refreshed every 5 secs, using a XML parsing. After refreshing, the current position of the list go back to first position. Is there any possible ways to solve this?
By using setAdapter() to update a list Android resets the position. Try altering the Adapter instead.
I don't know which adapter you're using so here two examples.
ArrayAdapter
adapter = list.getAdapter();
if (adapter == null) {
// Create new adapter + list.setAdapter()
} else {
adapter.clear();
adapter.addAll(newData);
adapter.notify(notifyDataSetChanged());
}
CursorAdapter
adapter.changeCursor(newCursor);
Use lv.smoothScrollToPosition(pos)
where pos could be any int, preferably the length of the adapter, if you want the listview to autoscroll to the last added entry as it is refreshed.
public void smoothScrollToPosition (int position)
Smoothly scroll to the specified adapter position. The view will scroll such that the indicated position is displayed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With