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
     Globals.song = items[position];
     title.setText(Globals.artist + " - " + Globals.song);
     seek.setProgress(0);

    //Then this should happen
     Music.playSong(Music.youtubeMp4(Globals.artist,Globals.song));
    }

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
 http://sites.google.com/site/rezmobileapps/treking 

--


Sponsored Links


Other Threads

1. Open Home v4.

Requirements: Android
Overview: Open Home Android App allows you to customize your Android phone
desktop
[image: Image]

Supporting multiple screens to place icons for apps or widgets (Clock,
Google Search, Battery Status, Weather, etc.). Open Home Android App
features a touch screen virtual keyboard to type keywords into the Google
Search Widget. Open Home supports auto-rotate and Skin chooser, so you can
browse skins in the Android Market to further customize you phone. One of
the stand-out features of Open Home is ive Folders which is an Android
firmware 1.5 feature, you can display any source of data on the Home screen
without forcing the the launch of an application. A Live Folder can be used
to display all your contacts, your bookmarks, your email, your play-lists,
an RSS feed, etc.


More Info:
http://betterandroid.wordpress.com/

Download Links (pilih salah satu):
- http://depositfiles.com/files/moiax4p0l
- http://www.megaupload.com/?d=RVJLKMN8


-- 
Deska Affif
P: 0818-177-172 | Y!/t/Ping!: GadgetGaya
http://GadgetGaya.com  |  Gadget Accessories Online Store

-- 
"Indonesian Android Community [id-android]" 

2. Parah nih Motorola..

Motorola MILESTONE users now ANGRY about lack of open bootloader access
http://www.eurodroid.com/2010/02/motorola-milestone-users-now-angry-about-lack-of-open-bootloader-access/

sent from Milestone..The Phone without Compromise..

-- 
"Indonesian Android Community [id-android]" 

3. How to use RTP in Android?

4. Why 'system' user cannot access /sdcard, follow-up question

5. linux "readahead" system call is not defind in android

6. Downloading App from store

7. Shared Heap concept in Android..