Updating a View with new data

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Updating a View with new data

Postby grbrg » Mon Jul 13, 2009 2:19 pm

Hi all,

I'm currently playing around with views, most of the time with views that change their content over time. In my test I ran into two problems, both related to invalidating the lists:

1. On a ButtonView, when changing the text, I call postInvalidate() since the update happens in a non UI-thread. It updates the text, but does not update the height of the button in case the text now requires more or less lines.

2. In a list, a separate thread is loading data into the list regularly. However, even when updating the list adapter (or even setting a new one) and calling postInvalidate() I get an exception "CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its view."

Seems I am doing something fundamentally wrong here in both cases. Maybe I do not understand how to handle views with changing data, so I'd be happy to hear what I should do differently. Thanks in advance!
grbrg
Developer
Developer
 
Posts: 34
Joined: Mon Jul 13, 2009 1:55 pm

Top

Postby mangaluve » Mon Jul 13, 2009 2:58 pm

Hello!

1. invalidate()/postInvalidate() will only redraw the button. To change the size, you could call requestLayout() which will schedule a new layout-pass, in which all the Views are measured.

2. Are you getting the exception when you call setAdapter or postInvalidate()? Im not sure whether or not you can call postInvalidate from another thread than the UI-thread, but I guess you can do it because of your first questions. However, changing the ListAdapter from another thread might be dangerous.

The Activity-class has a method called runOnUIThread which takes a Runnable as an argument. It's preferable to always use this method when modifying Views. If you call it from the UI-thread, the Runnable will be executed immediatly. However, if you do it from another thread, it will be posted in the message queue of the UI thread. In general you cannot modify Views from other thread (this would be dangerous, what if the UI thread is doing something with the views and, concurrently, another thread modifies it? Synchronization problems!)
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby grbrg » Mon Jul 13, 2009 4:20 pm

Hi mangaluve,

thanks for your response, it seemed to have brought me in the right direction. I now use Activity.runOnUIThread() to schedule a invalidate() and requestLayout(), which seems to do the job for my list. Seems I did miss calling requestLayout() previously, so my list of 0 elements (during onCreate) never displayed the items that were added later on.

I now get a NullPointerException when trying to scroll, but that's a topic for a different thread. Thanks for your help!
grbrg
Developer
Developer
 
Posts: 34
Joined: Mon Jul 13, 2009 1:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests