2

I have an array adapter which is used in my listview. The adapter is periodically updated by fetching or removing contents from a server. I have used a scheduledthreadpoolexecutor to periodically update the adapter and then use adapter.notifydatasetchange();

The list view gets refreshed and removes any items etc, but for example if two items where removed from the list when I scroll the listview on android and get close to the end of the listview the application crashes. I guess something does not get updated in the listview and it things that the size of the list is the initial size.

Do you have something to recommend?

Regards, Aris

Hi all,

I actually found a solution to my problem and forgot to check here for any replies. Thank you all for your suggestions. Basically scheduledthreadpoolexecutor called a runnable (lets call it updateRunnable) to do the updates. What I did was the following: In the updateRunnable, when it gets the new data and stores them in the array adapter, it then calls another runnable (lets call it updateListView) using runOnUiThread and in updateListView I set the adapter of the listview. This solved my problem

Aris
  • 21
  • 3
  • There is likely something wrong with the adapter. Make sure that you override getCount in the adapter to return the updated data set size – Hmmm Jan 29 '18 at 04:13

2 Answers2

1

If your data is at all database-like, which I assume, given your use of a ListView, then you'll want to refactor your background service into a model that uses a ContentProvider and SyncAdapter to stay in sync with the server, and then automatically notify the ListView through binding it with a CursorAdapter which uses its implementation of ContentObserver to automatically update the list when the underlying DB changes.

Why does ContentResolver.requestSync not trigger a sync? tells you how to set up the ContentProvider.

How to handle REST calls, data persistence, syncing and observing ContentProvider tells you a little more about how list update notification operates once the ContentProvider is syncing.

It's a lot of infrastructure work to get set up, but once you do, there's so much that's wonderfully automatic about the SyncAdapter model.

Community
  • 1
  • 1
jcwenger
  • 11,383
  • 1
  • 53
  • 65
  • I was wondering if you could help me with this question http://goo.gl/d5opg5 . It's not one of those "fix-this-for-me" questions, I promise. – Axel Jun 22 '15 at 03:50
0

I had a similar problem once. Since the ListView keeps updating you can

1) display the Listview just as the activity starts in OnCreate, and

2) call this SAME activity so as to display refreshed data in the listview.

but after calling the same activity again, finish() the current instance first immediately since you can get multiple instances of it one over the other.

sledge-hamster
  • 77
  • 1
  • 4
  • 12