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. Send/receiving files via Bluetooth

I would like my application to be able to send and receive files via
OBEX on Eclair. We have our own IANA MIME type.

My investigations of the (undocumented) Bluetooth APIs seem to suggest
that Eclair is hard-coded to only allow transfers of images, audio and
video. From the Bluetooth package manifest:

     <action android:name="android.intent.action.SEND" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:mimeType="image/*" />
     <data android:mimeType="video/*" />
     <data android:mimeType="audio/*" />

...and from the OBEX server itself:

  public static final String[] ACCEPTABLE_SHARE_INBOUND_TYPES =
      new String[] {

Is this true? If so, is there any way around this restriction? Our MIME
type is "application/...", so unless we can somehow persuade the system
to handle other MIME types, we're pretty much stuffed.

€€  €€€€€ €€€€€
life  .^3 4=+/,1 0 1 0 1}
--- Conway's Game Of Life, in one line of APL

2. Creating build.xml stops Eclipse compilation

I use Eclipse for developing, but want a build.xml in my project so
people can compile without it.
However, after I create the ant file with "android update project..",
Eclipse refuses to run my code since it does not find a target in the
ant file.

Eclipse really shouldn't care about this since it's not using ant to
build - but that's a different issue...

What is the easiest way to fix this problem?


3. putInt force closes

4. use of fadeScrollbars

5. Cari Casing Motorola Droid

6. editor or SVG converter?

7. Gmail ask password habis hidupin spica