简体   繁体   中英

create own Listener in android

i want to add listener to my class below :

    class BitmapDisplay implements Runnable
    {
        IAsyncFetchListener fetchListener = null;
        public void setListener(IAsyncFetchListener listener) {
            this.fetchListener = listener;
        }
        Bitmap bitmap;
        BitmapToLoad bitmapToLoad;
        public BitmapDisplay(Bitmap b, BitmapToLoad p){bitmap=b;bitmapToLoad=p;}
        public void run()
        {
            if(bitmap!=null)
            returnbitmap=bitmap;
            else
            returnbitmap=BitmapFactory.decodeResource(context.getResources(), stub_id);

            this.fetchListener.onComplete(returnbitmap);
        }
    }
}

but my listener wont work in eclipse

imageLoader.DisplayBitmap("").setListener(new IAsyncFetchListener() {
    @Override
    public void onComplete(Bitmap bitmap) {
        photoView.setImageBitmap(bitmap);
    }
});

I get an error in setListener :

The method setListener(new IAsyncFetchListener(){}) is undefined for the type Bitmap" how to solve it?

Because when you do imageLoader.DisplayBitmap("") , the method DisplayBitmap("") return a Bitmap .

You are mixing the method DisplayBitmap with the object BitmapDisplay .

But you can be able to do that :

BitmapDisplay bd=new BitmapDisplay(bmp, bitmapToLoad);
bd.setListener(new IAsyncFetchListener() {
            @Override
            public void onComplete(Bitmap bitmap) {
                photoView.setImageBitmap(bitmap);
            }
          }
);

Change your code to following:

DisplayBitmap function

public Void DisplayBitmap(String url,IAsyncFetchListener listener)
{
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null)
        listener.onComplete(bitmap);
    else
    {
        queueBitmap(url,listener);
        listener.onComplete(BitmapFactory.decodeResource(context.getResources(), stub_id));
    }
}

queueBitmap function

private void queueBitmap(String url,IAsyncFetchListener listener)
{
    BitmapToLoad p=new BitmapToLoad(url);
    executorService.submit(new BitmapsLoader(p,listener));
}

BitmapsLoader constructor

 IAsyncFetchListener listener;
 BitmapsLoader(BitmapToLoad bitmapToLoad,IAsyncFetchListener listener){
        this.bitmapToLoad=bitmapToLoad;
        this.listener=listener;
 }

Run method

    @Override
    public void run() {
        try{
            Bitmap bmp=getBitmap(bitmapToLoad.url);
            memoryCache.put(bitmapToLoad.url, bmp);
            BitmapDisplay bd=new BitmapDisplay(bmp, bitmapToLoad);
            bd.setListener(listener);
            handler.post(bd);
            }catch(Throwable th){
            th.printStackTrace();
        }
    }

And finally call this:

imageLoader.DisplayBitmap("",new IAsyncFetchListener() {
    @Override
    public void onComplete(Bitmap bitmap) {
       photoView.setImageBitmap(bitmap);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM