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

查看:132
本文介绍了使用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);
        }
    }
}

我没有测试此code,但它应该给你一个想法。

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

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

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