Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 包含图像的惰性列表。如何取消线程?_Android_Listview_Lazy Loading - Fatal编程技术网

Android 包含图像的惰性列表。如何取消线程?

Android 包含图像的惰性列表。如何取消线程?,android,listview,lazy-loading,Android,Listview,Lazy Loading,我从Fedor找到了这个例子,它绝对适合我的需要 我有个问题。如果在“清除缓存”按钮旁边有一个带有“取消”的按钮。我怎样才能在onClick中取消UI中的图像下载线程 多谢各位 编辑: 我认为它已经实现了这种方法: public void stopThread() { photoLoaderThread.interrupt(); } 但我不知道如何从UI线程访问它。在UI线程中,我只执行以下操作: adapter=new LazyAdapter(ctx, som

我从Fedor找到了这个例子,它绝对适合我的需要

我有个问题。如果在“清除缓存”按钮旁边有一个带有“取消”的按钮。我怎样才能在onClick中取消UI中的图像下载线程

多谢各位

编辑: 我认为它已经实现了这种方法:

public void stopThread()
    {
        photoLoaderThread.interrupt();
    }
但我不知道如何从UI线程访问它。在UI线程中,我只执行以下操作:

adapter=new LazyAdapter(ctx, someString);
setListAdapter(adapter);
在LazyAdapter中:

public View getView(int position, View convertView, ViewGroup parent) {
...
 imageLoader.DisplayImage(imagePath, activity, holder.image);
        return vi;
}
在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();
    }
但我认为一个明确的方法是从


谢谢您的帮助。

您可以编写自己的自定义类来扩展Java线程类。在这里,您实现了一个公共停止方法,用于停止线程本身。创建和启动线程时,您持有对它的引用,并在cancel按钮的OnClickEventHandler中调用public stop方法

只要把

adapter.imageLoader.stopThread();

要“取消”按钮,请单击处理程序

是否可以显示实例化线程对象的代码。你从哪里开始呢?啊,我真傻。现在,我已经在具有该命令的ui上看到了onDestroy overrite方法。Fedor,我非常感谢你的例子,它在我的应用程序中帮助了我很多,它就像一个魅力。谢谢,谢谢。