Code executing out of order?

by scp89 » Fri, 23 Apr 2010 08:05:41 GMT

Sponsored Links
 I am fairly new to android (and Java for that matter). I have an
onClick fuction for my listview. What I want it to do is changing the
text of a textview, set a seekbar progress to 0, and then load a new
song. The problem is that it loads the new song (which takes a while),
and THEN changes the textview and seekbar.

I want the textview and seekbar to change right away, then load the
song. Here's my function:

   public void onListItemClick(ListView parent, View v, int position,
long id) {

      //This stuff should happen immediately = items[position];
     title.setText(Globals.artist + " - " +;

    //Then this should happen

Like I said, the textview and seekbar doesn't change until the song
url is retrieved, and then the song buffers and starts. They should
change right away, since that code is first. Can someone explain why
this is and how to prevent it?

I want the textview and seekbar to change right away so the user has
some feedback when they click, otherwise it looks like nothing happens
for a few seconds while the song is loading.


Code executing out of order?

by ~ TreKing » Fri, 23 Apr 2010 10:48:05 GMT


Although you call setText first, you won't actually see that reflected on
the screen until the system does a render pass on the view. This happens on
the main thread. Unfortunately you're also calling the Music.playSong on the
main thread, which *blocks* until it's finished, then allows the main thread
to continue. Only then does the render pass complete at which point you see
the text changed. So the order of execution is something like this:

- request change in text (does not actually change view)
- load song (process, download, buffer, whatever, it takes time)
- system refreshes display with new text set in the first call

I hope that makes sense.

I want the textview and seekbar to change right away so the user has some

You'll want to put the music loading in a separate thread, or use Android's
threading facilities to simplify this (AsyncTask for example).

HOWEVER - since you said you new to Java it is HIGHLY recommended you learn
that first independently of Android. If you try to do both, after you
already got stuck on a classic threading problem, you are setting yourself
up for a lot of frustration.

Good luck.

TreKing - Chicago transit tracking app for Android-powered devices 


Sponsored Links

Other Threads

1. Question regarding WebView

Quick question,

I have started to play with the WebView and inserted one in a
LinearLayout just below another LinearLayout containing some other
widgets (buttons, text fields...)
All work fine.
When I pass a URL to the WebView, the content gets correctly
displayed. Now when I click any links in the displayed web page, the
entire view/screen is replaced by the browser, therefore the widgets
in the upper Layout are not visible anymore.
Question: Is there a way to force ths user to browse and stay in the
original WebView rather than having the system passing it to the main
browser? Is that managed via Intents as the activity that listens for
web intents in Android is the main browser?

Thanks for any help



2. Android SDK / emulator working fine on Linux 64 bits ?


I plan to upgrade my PC and move from Mandriva 32bits to Mandriva

Are Eclispe & SDK tools & the emulator still working fine ?

Any specific remarks on such a platform ?

3. about the sdk 1.0 source code

4. M3 SDK: Click events on Map Overlays?

5. Please help me - NoClassDefFoundError: -

6. G1's arriving early to some US customers...

7. Please help me - NoClassDefFoundError: -