I have an ap开发者_开发技巧plication that uses images from web a a source. for my application I'm first downloading images in seperate thread and only if image exists on disk I show on the list. Downloader is a seperate thread, but I'm very new using threads and now something else is confusing me. My images are not shown unless I refresh the view, that is my list. Now I needed to show my images so I wrote this in my custom view :
private Runnable LoaderTask=new Runnable(){
public void run() {
if(cellIcon!=null){
if(iconCache.get(cellIcon)!=null){
icon.setImageBitmap(iconCache.get(cellIcon));
mHandler.postDelayed(this, 200);
}
}
}
};
private class ImageThread extends Thread{
@Override
public void run() {
// mHandler.removeCallbacks(LoaderTask);
mHandler.postDelayed(LoaderTask, 100);
}
}
And I trigger my thread like this
ImageThread thread=new ImageThread();
thread.start();
the handler is instantiated in the beginning of the class. But when I do this n othing happen until I refresh my views. I tried (Activity).runOnUIThread inside one of my threads I got stackoverflow error. How do I solve that or are there any other advices.you can give me? Thanks
Use AsyncTask. In onPostExecute() method just call a method from the activity and refresh the UI. The onPostExecute() is called when the thread is done.
http://developer.android.com/resources/articles/painless-threading.html
P.S. To call an activity's method just use Interface.
Have a look at this:
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
精彩评论