PravinCG
PravinCG

Reputation: 7708

ListView refresh taking it to the top of the list

Scenario: I am using a listview with custom Adapter. I am fetching data every 5 seconds and then displaying it in the listview.

Problem: After every refresh the list scrolls at the top. I would want the list to stay as is from the scroll standpoint and just the underlying data to be refreshed.

Current code: This is called every time I fetch a new data.

mListAdapter = new CustomListAdapter(this, mListData);
mList.setAdapter(mListAdapter);

Upvotes: 0

Views: 2047

Answers (5)

Danial
Danial

Reputation: 1

This Worked For Me:

adapter.setData(list);
adapter.notifyDataSetChanged();

Upvotes: 0

san
san

Reputation: 1835

You can use

mList.setSelection(mListData.size() - CONSTANT.CONTENT_SIZE);

This way the listview will not refresh to the top. Or you can also use

adapter.notifydatasetchanged

by updating the binded list.

Upvotes: 0

Dany's
Dany's

Reputation: 928

Instead of your code please try to put every time you fetch data this one:

adapter.notifyDataSetChanged();

Upvotes: 1

Pratik
Pratik

Reputation: 30855

As you get new mListData then just invalidate the adapter not re-initialized the adapter you can call like this

mListAdapter.notifyDataSetChanged();

Upvotes: 1

Dimitris Makris
Dimitris Makris

Reputation: 5183

Instead of calling this, you should set the new data to the adapter and then call notifyDataSetChanged.

Hope this helps!

Upvotes: 1

Related Questions