开发者

Lazy List with Images. How to cancel thread?

I have found this example Lazy load of images in ListView from Fedor which is absolutely great for what I need.

I have a question. if beside the Clear Cache button there would be a button with Cancel. How could I in onClick cancel the image download thread from the UI ?

Thank you.

Edit: I think that it already has this method implemented:

public void stopThread()
    {
        photoLoaderThread.interrupt();
    }

But I don't know how to access it from the UI Thread. In UI Thread I only do this:

adapter=new LazyAdapter(ctx, someString);
setListAdapter(adapter);

in LazyAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
...
 imageLoader.DisplayImage(imagePath, activity, holder.image);
        return vi;
}

and in ImageLoader

public void DisplayImage(String url, Activity activity, ImageView imageView)
    {
        if(cache.containsKey(url))
            imageView.setImageBitmap(cache.get(url));
        else
        {
            queuePhoto(url, activity, imageView);
            imageView.setImageResource(stub_id);
        }    
    }

private void queuePhoto(String url, Activity activity, ImageView imageView)
    {
        //This ImageView may be used for other images b开发者_StackOverflowefore. So there may be some old tasks in the queue. We need to discard them. 
        photosQueue.Clean(imageView);
        PhotoToLoad p=new PhotoToLoad(url, imageView);
        synchronized(photosQueue.photosToLoad){
            photosQueue.photosToLoad.push(p);
            photosQueue.photosToLoad.notifyAll();
        }

        //start thread if it's not started yet
        if(photoLoaderThread.getState()==Thread.State.NEW)
            photoLoaderThread.start();
    }

But I think an clear way is to download the sources from link text

Thank you for your help.


Just put

adapter.imageLoader.stopThread();

to "Cancel" button click handler


You can write your own custom class which extends the Java Thread class. There you implement a public stop-method which stop the thread itself. When you create and start your thread you hold a reference to it and call the public stop-method in the OnClickEventHandler of the cancel button.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜