Android update UI

2019-07-29 22:42发布

问题:

Basically at the moment I'm showing a loading screen (loading.setVisibility(View.VISIBLE);) then loading some external data using a "BufferedReader" and then hiding the loading screen (loading.setVisibility(View.INVISIBLE);) afterwards. The problem is the loading screen is never shown. The code is all in the correct place the GUI just doesn't update until the end of the method when it is obviously too late because the loading screen is set to invisible again.

Is there any way to force update the UI mid way through a method?

Any help would be much appreciated!

回答1:

You can't do this without using a background thread. There are a number of ways you can do this on Android, but the simplest is to use AsyncTask

It's worth reading Painless threading for more information on this.

Hope this helps,

Phil Lello



回答2:

I think, you should read about AsyncTask class in Android, then take a look this example: http://labs.makemachine.net/2010/05/android-asynctask-example/