tags:

views:

308

answers:

1

I'm running into an IllegalStateException updating an underlying List to an Adapter (might be an ArrayAdapter or an extension of BaseAdapter, I don't remember). I do not have or remember the text of the exception at the moment, but it says something to the effect of the List's content changing without the Adapter having been notified of the change.

This List /may/ be updated from another thread other than the UI thread (main). After I update this list (adding an item), I call notifyDataSetChanged. The issue seems to be that the Adapter, or ListView attached to the Adapter attempts to update itself before this method is invoked. When this happens, the IllegalStateException is thrown.

If I set the ListView's visibility to GONE before the update, then VISIBLE again, no error occurs. But this isn't always practical.

I read somewhere that you cannot modify the underlying this from another thread--this would seem to limit an MVC pattern, as with this particular List, I want to add items from different threads. I assumed that as long as I called notifyDataSetChanged() I'd be safe--that the Adapter didn't revisit the underlying List until this method was invoked but this doesn't seem to be the case.

I suppose what I'm asking is, can it be safe to update the underlying List from threads other than the UI? Additionally, if I want to modify the data within an Adapter, do I modify the underlying List or the Adapter itself (via its add(), etc. methods). Modifying the data through the Adapter seems wrong.

I came across a thread on another site from someone who seems to be having a similar problem to mine: http://osdir.com/ml/Android-Developers/2010-04/msg01199.html (this is from where I grabbed the Visibility.GONE and .VISIBLE idea).

To give you a better idea of my particular problem, I'll describe a bit of how my List, Adapter, etc. are set up.

I've an object named Queue that contains a LinkedList. Queue extends Observable, and when things are added to its internal list through its methods, I call setChanged() and notifyListeners(). This Queue object can have items added or removed from any number of threads.

I have a single "queue view" Activity that contains an Adapter. This Activity, in its onCreate() method, registers an Observer listener to my Queue object. In the Observer's update() method I call notifyDataSetChanged() on the Adapter.

I added a lot of log output and determined that when this IllegalStateExcption occurs that my Observer callback was never invoked. So it's as if the Adapter noticed the List's change before the Observer had a chance to notify its Observers, and call my method to notify the Adapter that the contents had changed.

So I suppose what I'm asking is, is this a good way to rig-up an Adapter? Is this a problem because I'm updating the Adapter's contents from a thread other than the UI thread? If this is the case, I may have a solution in mind (give the Queue object a Handler to the UI thread when it's created, and make all List modifications using that Handler, but this seems improper).

I realize that this is a very open-ended post, but I'm a bit lost on this and would appreciate any comments on what I've written.

+2  A: 

This List /may/ be updated from another thread other than the UI thread (main)

That won't work.

I read somewhere that you cannot modify the underlying this from another thread--this would seem to limit an MVC pattern, as with this particular List, I want to add items from different threads

MVC has nothing to do with threads.

can it be safe to update the underlying List from threads other than the UI?

No. Other threads can trigger updates to the adapter (e.g., via post()), but the updates themselves must be processed on the main application thread, for an adapter that is currently attached to a ListView.

Additionally, if I want to modify the data within an Adapter, do I modify the underlying List or the Adapter itself (via its add(), etc. methods). Modifying the data through the Adapter seems wrong.

You modify your Adapter via the Adapter itself for ArrayAdapter. You modify your Adapter via the underlying database/content provider for CursorAdapter. Other adapters may vary.

I've an object named Queue that contains a LinkedList. Queue extends Observable, and when things are added to its internal list through its methods, I call setChanged() and notifyListeners().

Have you considered using LinkedBlockingQueue, rather than implementing your own thread-safe Queue?

This Activity, in its onCreate() method, registers an Observer listener to my Queue object. In the Observer's update() method I call notifyDataSetChanged() on the Adapter.

Adapters should call notifyDataSetChanged() on themselves (if the change is made by them) or have it called on them by the entity that is changing the data (e.g., a Cursor for a CursorAdapter). That is MVC. The Activity should neither know nor care when your data model changes.

So it's as if the Adapter noticed the List's change before the Observer had a chance to notify its Observers, and call my method to notify the Adapter that the contents had changed.

Possibly you are using an ArrayAdapter, in which case all this extra observer/notify stuff is getting in your way, since that's handled for you. You just need to arrange to update the ArrayAdapter on the main application thread.

So I suppose what I'm asking is, is this a good way to rig-up an Adapter?

Not particularly, IMHO.

Is this a problem because I'm updating the Adapter's contents from a thread other than the UI thread?

If you aren't forcing the updates back to the main application thread, this will eventually crash, once you clear up your other problems.

give the Queue object a Handler to the UI thread when it's created, and make all List modifications using that Handler, but this seems improper

You could use a Handler, or you could call post() on your attached ListView.

Off the cuff, I'd create a subclass of ArrayAdapter named ThreadSafeArrayAdapter and use it in place of Queue. ThreadSafeArrayAdapter replaces add(), insert(), and remove() with ones that have the superclass do its thing on the main application thread, via a Handler or post().

CommonsWare
Thanks for your comments, but I'm still confused on a few things, if you wouldn't mind helping further.The "Queue" object name is a bit of a misnomer--it's more of a media queue of videos to play, or songs to play. It's a list of items with a currentIndex int.I understood the Adapter to be just a bridge between my Model (my List) and view (ListView). What if I need to update the data that the view displays from another Activity where I do not have access to the Adapter? It would seem natural to update the underlying List, perhaps from a Runnable if the task performing this add could block.
kpdvx
"I understood the Adapter to be just a bridge between my Model (my List) and view (ListView)." That's generally true. "What if I need to update the data that the view displays from another Activity where I do not have access to the Adapter?" Most likely, you don't have access to the List either, then, so you will need a more elaborate strategy. "It would seem natural to update the underlying List, perhaps from a Runnable if the task performing this add could block." IMHO, the threading issue is a UI issue, which is why I recommend that your Adapter be the one to deal with thread safety.
CommonsWare
But, you're welcome to implement it however you want, so long as you only update the Adapter on the main application thread.
CommonsWare
After re-reading your reply, I see that "updates themselves must be processed on the main application thread, for an adapter that is currently attached to a ListView" answers the question in my comment. The scenario I'm confused about is this: my "Queue" object is created on the main thread from within a Service. The Service will periodically check a web service to find new songs to play. Because this process may block, this is done from within a separate Thread. At this point, from within the Thread I would add() the new item to the List. I suppose this should be done from a Handler on main.
kpdvx
"my "Queue" object is created on the main thread from within a Service." Just to be clear: that is the main application thread, since all components share a thread. "I suppose this should be done from a Handler on main." Handler or something, yes. It is merely a question of what hunk of code gets that responsibility.
CommonsWare
Yes, the Service is on the same thread as all the Activities (we're on the same page, I'm just confirming). I was running out of room so I couldn't be too descriptive. Since from within this Service I will have access to the List, but not to its Adapter (which may or may not be attached), the only way to be safe about this update, as I now understand it, is to use a Handler to the UI thread (the main thread) that is instantiated in the Service's onCreate() method. Is this a correct understanding?
kpdvx
I wouldn't build my app the way you're building yours, so I cannot comment about "the only way to be safe". I have not attempted to use a Handler created inside a Service, so I do not know how well that works.
CommonsWare
"I wouldn't build my app the way you're building yours" -- do you have any suggestions on how to restructure my app so that I wont have some of the problems that I've mentioned?
kpdvx
Sorry, I was in a foul mood when I wrote that. I am somewhat skeptical on just holding your data model in memory -- if it's worth downloading, it might be worth storing in a database. If so, then a fair bit of your approach will wind up changing. Android's support for true MVC is limited at best, and when you get to databases, the only sane approach is for the M to just be the database itself, meaning you have a very dumb model. You'd also wind up with a CursorAdapter of some form to wrap around your database query for representing the data on-screen, etc.
CommonsWare