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. Problem installing android sdk in ubuntu 10.10

Hi all,

I am trying to install android sdk, but the for every try, the android sdk
manager gives this error message:

"Failed to fetch URL
http://dl-ssl.google.com/android/repository/addons_list.xml, reason:
Connection timed out"

It may look silly, but has anybody solved this? And I have tried with and
without https!

Thanks in advance.


Regards,
Saurav Mukherjee.

-- 

2. FOTA-like updates for non-phone devices?

Guys:


Are there any docs available which suggest approaches for FOTA-like
updates to non-phone Android devices?

In a nutshell, I'm looking at deploying Android as the operating
system for a device that isn't a phone.  I'm trying to figure out what
the best way might be to do in-the-field updates of the device,
particularly ones that don't require end-user intervention.  Updates
of both APKs and the core system files are considered.

I understand the basics behind all of the above, I'm just wondering if
there is existing infrastructure within the Android framework (or
elsewhere) that I should be taking advantage of.  I have experience
with this stuff on non-Android platforms.


Thanks!


b.g.
-- 
Bill Gatliff
b...@billgatliff.com

-- 

3. Needed Help

4. Developer Console problem

5. Add frames to video stream

6. IM Application of Android 2.3.3

7. WindowManager$BadTokenException