使用 AsyncTask 将图像加载到自定义适配器中 [英] Using AsyncTask to load images into a custom adapter

查看:22
本文介绍了使用 AsyncTask 将图像加载到自定义适配器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有很多教程,但我发现实现 AsyncTask 以将图像从 URI(从内容提供者获得)加载到自定义适配器中真的很困难.

Although there are many tutorials out there, I've found it really difficult to implement an AsyncTask to load images from URI's (obtained from a content provider) into a custom adapter.

我得到了基本要点,即有一个包含 AsyncTask 的类,在doInBackground"中创建位图,然后在 onPostExecute 中设置 ImageView.

I get the basic gist, which is to have a class containing an AsyncTask, do the bitmap creation in the 'doInBackground', and then set the ImageView in the onPostExecute.

对我来说的问题,我是 android 的新手 &编程,就是我不知道如何将我的Uri 传递给每个项目的AsyncTask,如何创建位图,以及如何将其返回给适配器.

The problem for me, being new to android & programming, is that I don't know how to pass in my Uri's to the AsyncTask for each item, how to create the bitmap, and how to return it to the adapter.

对于实际的 AsyncTask 类 (ImageLoader),我只做到了这一点:

I've only gotten this far with the actual AsyncTask class (ImageLoader):

package another.music.player;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;

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

    private ImageView imageView;
    private Bitmap bitmap = null;

    @Override
    protected Bitmap doInBackground(String... uri) {

        // Create bitmap from passed in Uri here

        return bitmap;

    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null && imageView != null) {
            imageView.setImageBitmap(bitmap);

        }
    }
}

我的自定义适配器如下所示:

And my custom adapter looks like this:

package another.music.player;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore.Audio.AlbumColumns;
import android.provider.MediaStore.Audio.AudioColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

class AlbumAdapter extends CursorAdapter {

    public AlbumAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    private static Uri currentSongUri;

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView albumArt = (ImageView) view.getTag(R.id.albumArt);
        TextView text1 = (TextView) view.getTag(R.id.artistTitle);
        TextView text2 = (TextView) view.getTag(R.id.albumTitle);
        TextView text3 = (TextView) view.getTag(R.id.totalSongs);

        albumArt.setImageBitmap(null);

        text1.setText(cursor.getString(cursor
                .getColumnIndex(AudioColumns.ARTIST)));
        text2.setText(cursor.getString(cursor
                .getColumnIndex(AudioColumns.ALBUM)));
        text3.setText(cursor.getString(cursor
                .getColumnIndex(AlbumColumns.NUMBER_OF_SONGS)));

        String currentAlbumId = cursor.getString(cursor
                .getColumnIndex(BaseColumns._ID));

        Integer currentAlbumIdLong = Integer.parseInt(currentAlbumId);
        Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
        currentSongUri = ContentUris.withAppendedId(artworkUri,
                currentAlbumIdLong);

        //Run ImageLoader AsyncTask here, and somehow retrieve the ImageView & set it.

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.albumitem, null);
        view.setTag(R.id.albumArt, view.findViewById(R.id.albumArt));
        view.setTag(R.id.artistTitle, view.findViewById(R.id.artistTitle));
        view.setTag(R.id.albumTitle, view.findViewById(R.id.albumTitle));
        view.setTag(R.id.totalSongs, view.findViewById(R.id.totalSongs));

        return view;
    }

}

如果有人能告诉我如何进行此操作,我将不胜感激.

I would be very grateful if somebody could show me how to proceed with this.

谢谢.

推荐答案

您需要将视图传递给 AsyncTask 以便它可以在完成时更新它:

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);

并修改AsyncTask使其可以处理混合参数类型:

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.

这篇关于使用 AsyncTask 将图像加载到自定义适配器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆