I have Activity
that displays a ListView
, which is associated with Adapter
"backed by" ArrayList
global. If I add an element to this ArrayList
, ideally I do it on the main thread and immediately call Adapter.notifyDataSetChanged()
to avoid an "IllegalStateException: The content of the adapter has changed but ListView did not receive a notification ". However, this ArrayList
is changed by a secondary thread, executed by a Service
that is not always coupled to Activity
and therefore does not always have a reference to Adapter
so that it can ask the main thread to execute these two operations . And then when (I imagine) the main thread comes across the changed list, the exception ends up happening.
I see two alternatives to resolve this:
Make Adapter
global so I can call you whenever I want.
Create a deep copy from the list and associate the copy with Adapter
. So when the original list is changed the copy remains untouched until the main thread needs to display the change (in Activity.onResume()
, for example). Then I change the copy from the original and I call notifyDataSetChanged()
.
The disadvantage of the first one in my opinion is to make the code confusing with the presence of an object outside its correct scope, because the strictest thing would be to keep it for the lifetime of the Activity
that uses it. The disadvantages of the second are the redundancy that seems to be unnecessary and the extra memory occupancy (although the list is not very large, at most about 500 objects of a 12 fields each). Are there other advantages and disadvantages in these two options? Is there a third option?
I've also posted on SO in English .