views:

66

answers:

2

I have found this example http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012 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 before. 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.

+1  A: 

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.

Flo
Can you show the code where you instantiate the Thread object. And where you start the thread?
Flo
+1  A: 

Just put

adapter.imageLoader.stopThread();

to "Cancel" button click handler

Fedor
Ahhhhh, silly me. Right now I've seen the onDestroy overrite method on the ui that had the command. Fedor, I would like to greatly thank you for your example, it helped me a lot in my application and it works like a charm. Thank you, thank you.
Alin