[英]Call to AsyncTask from custom adapter
I have created a listview with a custom adapter. 我用自定义适配器创建了一个listview。 One of the fields is an image to show the avatar of each user.
字段之一是显示每个用户的化身的图像。 I must obtain those images from an url.
我必须从网址获得那些图像。
I have created a class that converts an image from URL into a Bitmap. 我创建了一个将图像从URL转换为位图的类。
I think this should be done from an asyntask. 我认为这应该从异步任务完成。 The problem is that I do not know how to call this method from a custom adapter.
问题是我不知道如何从自定义适配器调用此方法。
This is my class: 这是我的课:
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;
}
}
This return a Bitmap. 这将返回一个位图。 Then from my custom adapter, i need to put that Bitmap in a ImageView
然后从我的自定义适配器,我需要将该位图放入ImageView
for example, i'm trying: 例如,我正在尝试:
ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
avatarView.setImageBitmap(new obtAvatar2().execute());
But, it's wrong :( 但是,这是错误的:(
any advice? 有什么建议吗?
I suggest to you to either work with Glide or Picasso libaries, they are the most used image library on android application : 我建议您使用Glide或Picasso库,它们是android应用程序中使用最多的图像库:
To import to you project with gradle : 要使用gradle导入到您的项目中:
PICASSO : 毕加索
dependencies {
compile 'com.squareup.picasso:picasso:2.5.1'
}
GLIDE : 滑行 :
dependencies {
compile 'com.github.bumptech.glide:glide:3.5.2'
}
Usage : 用法:
PICASSO : 毕加索
Picasso.with(myFragment)
.load(url)
.into(myImageView);
GLIDE : 滑行 :
Glide.with(myFragment)
.load(url)
.into(myImageView);
Hope this helps 希望这可以帮助
You can use Glide or Picasso . 您可以使用Glide或Picasso 。 As those are very helpful libraries for setting image in adapter (here views are reusable).
因为这些库对于在适配器中设置图像非常有用(这里的视图可重用)。
If you still want to use asynctask then check below: 如果您仍然想使用asynctask,请检查以下内容:
In adapter each time scroll will lead to new network call, that can be avoided using saving bitmap object. 在适配器中,每次滚动都会导致新的网络调用,可以使用保存位图对象来避免这种情况。
You are trying to get image by using below code: 您正在尝试使用以下代码获取图像:
ImageView avatarView = (ImageView)view.findViewById(R.id.imageViewAvatarMensa);
avatarView.setImageBitmap(new obtAvatar2().execute());
This will not work as: 这将不能用作:
new obtAvatar2().execute()
It will execute in background and return response in onPostExucute() . 它将在后台执行,并在onPostExucute()中返回响应。 And result is:
结果是:
avatarView.setImageBitmap(null)
If you want to use asytask then probably you need make your code like: 如果您想使用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
}
}
You can pass reference of ImageView in constructor. 您可以在构造函数中传递ImageView的引用。
First of all you should add obtAvatar2 async task in your custom adapter. 首先,您应该在自定义适配器中添加obtAvatar2异步任务。
I hope you are using ViewHolder in your customadapter, then in you getView(), before assigning value to your Imageview, call the async task. 我希望您在customadapter中使用ViewHolder,然后在getView()中,将值分配给Imageview之前,调用异步任务。 For example:
例如:
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;
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.