I use a adapter with recycled views (holder). This is the code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.playlists_item, null);
holder = new ViewHolder();
holder.picture = (ImageView) convertView.findViewById(R.id.playlist_picture);
holder.name = (TextView) convertView.findViewById(R.id.playlist_name);
holder.desc = (TextView) convertView.findViewById(R.id.playlist_desc);
holder.play_bt = (ImageView) convertView.findViewById(R.id.playlist_play);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.picture.setImageResource(R.drawable.placeholder);
...
if(messages.get(position).containsKey("IMAGE"))
{
if(global.isInCache(messages.get(position).get("IMAGE")))
holder.picture.setImageBitmap(global.getBitmap(messages.get(position).get("IMAGE")));
else{
holder.picture.setTag("http://**********" + messages.get(position).get("IMAGE"));
new loadImageTask().execute(holder.picture,cache,messages.get(position).get("IMAGE"),"********/upload/********/");
}
}
And this is my downloader:
public class loadImageTask extends AsyncTask< Object , Void, Void>
{
...
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Object... params) {
imagem = (ImageView) params[0];
image_tag = (String)imagem.getTag();
caminho = (String) params[3];
name = (String) params[2];
imgLoad = LoadImageFromWeb(caminho+name);
return null;
}
@Override
protected void onPostExecute(Void result) {
if (imgLoad instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable)imgLoad).getBitmap();
}
bitmap = getResizedBitmap(bitmap, 100, 100);
global.putCache(name, bitmap);
imagem.setImageBitmap(bitmap);
}
}
It turns out that when I scroll in listview
when the download is finished, the image
is loaded me in the position visible (and in the same position of the listview
when scrolling is fast, the image is always being changed every time a download is finished).
I wish, if the position of the image that you are downloading to be done, is no longer visible, it is not done setImageBitmap