Android – Losing current selection of list item after updating the listview items using notifyDataSetChanged

androidandroid-widget

I need to refresh the ListView items every second, and it works fine using notifyDataSetChanged(). After the refresh, the already selected item in the list is losing its selection. ie, it loses its selected item background. How can I maintain the selected state of already selected item, after each refresh?

Thanking u for the support in advance.

Best Answer

As I understood your question your query is that you are losing the selected item focus of the ListView after refreshing the ListView. So, for that you can use ListView's setSelection() method to set the selected item in the ListView. So, when you refresh the ListView after that you can set the item that you want to be selected or the previous selected using setSelection().

listView.setSelection(position)

Here position is the position that was the last selected index before notifyDataSetChanged()

Related Topic