从自定义适配器调用AsyncTask

时间:2018-12-17 10:28:24

标签: android android-studio listview

我已经使用自定义适配器创建了一个列表视图。字段之一是显示每个用户的化身的图像。我必须从网址获取这些图像。

我创建了一个类,用于将图像从URL转换为位图。

我认为这应该从异步任务完成。问题是我不知道如何从自定义适配器调用此方法。

这是我的课程:

private class obtAvatar2 extends AsyncTask<Void , Void, Bitmap>{
    Bitmap bm;

    @Override
    protected Bitmap doInBackground(Void... voids) {
        try {

            URL url = new URL("https://www.bellatores.cl/wp-content/uploads/2018/01/Avatar-Mujer.png");
            URLConnection con = url.openConnection();
            con.connect();
            InputStream is = con.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();

        }catch (IOException e){

        }

        return bm;
    }
}

这将返回一个位图。 然后从我的自定义适配器中,我需要将该位图放入ImageView

例如,我正在尝试:

ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
avatarView.setImageBitmap(new obtAvatar2().execute());

但是,这是错误的:(

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

我建议您使用Glide或Picasso库,它们是android应用程序中使用最多的图像库:

要使用gradle导入到您的项目:

PICASSO

dependencies {
    compile 'com.squareup.picasso:picasso:2.5.1'
}

GLIDE

dependencies {
    compile 'com.github.bumptech.glide:glide:3.5.2'
}

用法:

PICASSO

Picasso.with(myFragment)
    .load(url)
    .into(myImageView);

GLIDE

Glide.with(myFragment)
    .load(url)
    .into(myImageView);

希望这会有所帮助

答案 1 :(得分:0)

您可以使用GlidePicasso。因为这些库对于在适配器中设置图像非常有用(这里的视图可重用)。

如果您仍要使用asynctask,请检查以下内容:

在适配器中每次滚动都会导致新的网络调用,可以使用保存位图对象来避免这种情况。

您正尝试使用以下代码获取图像:

ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
avatarView.setImageBitmap(new obtAvatar2().execute());

这将不能用作:

new obtAvatar2().execute()

它将在后台执行,并在 onPostExucute()中返回响应。结果是:

avatarView.setImageBitmap(null)

如果您想使用 asytask ,则可能需要编写如下代码:

private class obtAvatar2 extends AsyncTask<Void, Void, Bitmap> {
        Bitmap bm;

        @Override
        protected Bitmap doInBackground(Void... voids) {
            try {

                URL url = new URL("https://www.bellatores.cl/wp-content/uploads/2018/01/Avatar-Mujer.png");
                URLConnection con = url.openConnection();
                con.connect();
                InputStream is = con.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();

            } catch (IOException e) {

            }

            return bm;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
                avatarView.setImageBitmap(bitmap);
                //set bitmap to imageview and save in local list, so in future no need to download
            }
        }

您可以在构造函数中传递 ImageView 的引用。

答案 2 :(得分:0)

首先,您应该在自定义适配器中添加obtAvatar2异步任务。

我希望您在customadapter中使用ViewHolder,然后在getView()中使用,然后再将值分配给Imageview,然后调用异步任务。例如:

 public static class ViewHolder {

    public ImageView display_adImage;

}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    try {
        if (convertView == null) {

            vi = inflater.inflate(R.layout.test_layout, null);
            holder = new ViewHolder();

            holder.display_adImage = vi.findViewById(R.id.IvAdImage);


            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }

        ...


        Bitmap b =  new GetImageTask().execute().get();
    holder.display_adImage.setImageBitmap(b);
}
}

 private class obtAvatar2 extends AsyncTask<Void , Void, Bitmap>{
Bitmap bm;

@Override
protected Bitmap doInBackground(Void... voids) {
    try {

        URL url = new URL("https://www.bellatores.cl/wp-content/uploads/2018/01/Avatar-Mujer.png");
        URLConnection con = url.openConnection();
        con.connect();
        InputStream is = con.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();

    }catch (IOException e){

    }

    return bm;
}

}