从自定义适配器调用AsyncTask



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

我创建了一个类,可以将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());

但是,这是错误的:(

有什么建议吗?

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

使用渐变导入到您的项目:

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

希望这能帮助

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

如果您仍然想使用异步任务,请检查以下内容:

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

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

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的引用。

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

我希望您在自定义适配器中使用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;
}

}

最新更新