I need your help!
In my application, strange behavior when displaying images in GridView. The cell displays the image instead of the default R.id.noimage and previously viewed images. This bug appeared after I added in my adapter to load the bitmap AsyncTask.
What am I doing wrong?
Code:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View vi = null;
ImageView image;
if(convertView==null) {
vi = inflater.inflate(R.layout.gall_item, null);
Log.i("info","convertView==null");
} else {
vi = inflater.inflate(R.layout.gall_item, null);
Log.i("info","else");
}
holder.position = position;
image = (ImageView)vi.findViewById(R.id.img);
TextView url_img = (TextView)vi.findViewById(R.id.url_img);
TextView cer = (TextView)vi.findViewById(R.id.cer);
TextView idi = (TextView)vi.findViewById(R.id.ids);
url_img.setText(img2[position]);
cer.setText(cer_ar[position]);
idi.setText(id_ar[position]);
new LoadImageT(holder).execute(image, img1[position],activity.getApplicationContext(),position);
return vi;
}
class LoadImageT extends AsyncTask<Object, Void, Bitmap> {
Context context;
private ImageView imv;
private String url;
private Bitmap bitmap = null;
private int posS;
private ViewHolder mHolder;
public LoadImageT(ViewHolder holder) {
mHolder = holder;
}
@Override
protected Bitmap doInBackground(Object... params) {
imv = (ImageView) params[0];
url = (String) params[1];
context = (Context)params[2];
posS = (Integer) params[3];
bitmap = imageLoader.getBitmaptrue(url,imv,context);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if (mHolder.position == posS) {
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
} else {
int stub_id = R.drawable.noimage;
imv.setImageResource(stub_id);
}
}
}
you are doing it in a wrong manner, First of all as andy suggested to you if your view is null inflate the view and set the tag and then use this tag when its not null but it only effect your list view efficiency. Secondly remove your async task code from adapter put it into an activity and use the object. See this link Gilles explain it nicely how to do multi threading in an efficient way.