I'm using a similar approach to the answer of this question. The only real difference is that instead of a SoftReference<Bitmap
based cache, I'm saving the images to /data/data/my.app/files
since they're not expected to change too often. My adapter's getView()
function:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//data from your adapter
MyItem entry = getItem(position);
//we want to reuse already constructed row views...
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.appitem, null);
}
convertView.setTag(entry);
TextView Name = (TextView)convertView.findViewById(R.id.Name);
TextView Version = (TextView)convertView.findViewById(R.id.Version);
final ImageView Icon = (ImageView)convertView.findViewById(R.id.Icon);
Name.setText(entry.getName());
Version.setText(entry.getVersion());
Icon.setImageResource(R.drawable.default_icon); // the problem line
try {
final String _id = entry.getID();
imageLoader.loadImage(_id, "<my url>", new ImageThreadLoader.ImageLoadedListener() {
public void imageLoaded(Bitmap imageBitmap) {
Icon.setImageBitmap(imageBitmap);
notifyDataSetChanged();
}
});
} catch (Throwable t) {
Log.e("","", t); // nothing is hitting this log
}
return convertView;
}
The marked "problem line" above where I set the icon to a default icon, if I remove that line then things mostly work ok (when Views are reused it'll display the old image shortly before showing the new one). If that line is present then the image never changes to anything else. The anonymous ImageLoadedListener
is still run on the UI thread and setting a breakpoint there reveals everything seems to be happening normally. I also know that the ImageThreadLoader
is working correctly. The files appear where they should and look just fine (and they load fine when the problem line above is removed).
Why would setting the image ahead of time result in not being able to update it later?