Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 758997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:32:42+00:00 2026-05-14T15:32:42+00:00

I’m running into an IllegalStateException updating an underlying List to an Adapter (might be

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:32:42+00:00Added an answer on May 14, 2026 at 3:32 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.