Using AsyncTask to load images into a custom adapter

拜拜、爱过 提交于 2019-11-30 07:20:44

You need to pass your AsyncTask the view so that it can update it when it completes:

//Run ImageLoader AsyncTask here, and let it set the ImageView when it is done.
new ImageLoader().execute(view, uri);

And modify AsyncTask so that it can handle mixed parameter types:

public class ImageLoader extends AsyncTask<Object, String, Bitmap> {

    private View view;
    private Bitmap bitmap = null;

    @Override
    protected Bitmap doInBackground(Object... parameters) {

        // Get the passed arguments here
        view = (View) parameters[0];
        String uri = (String)parameters[1];

        // Create bitmap from passed in Uri here
        // ...
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null && view != null) {
            ImageView albumArt = (ImageView) view.getTag(R.id.albumArt);
            albumArt.setImageBitmap(bitmap);
        }
    }
}

I haven't tested this code but it should give you an idea.

Why are you doing setImage in AsyncTask? You can do it in a thread. I don't think in this condition AsyncTask would be good. Better you do it in different thread.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!