Web image loaded by thread in android
Posted
by Bostjan
on Stack Overflow
See other posts from Stack Overflow
or by Bostjan
Published on 2010-06-02T11:39:51Z
Indexed on
2010/06/02
11:44 UTC
Read the original article
Hit count: 290
I have an extended BaseAdapter in a ListActivity:
private static class RequestAdapter extends BaseAdapter {
and some handlers and runnables defined in it
// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
loadAvatar();
}
};
protected static void loadAvatar() {
// TODO Auto-generated method stub
//ava.setImageBitmap(getImageBitmap("URL"+pic));
buddyIcon.setImageBitmap(avatar);
}
In the getView function of the Adapter, I'm getting the view like this:
if (convertView == null) {
convertView = mInflater.inflate(R.layout.messageitem, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.username = (TextView) convertView.findViewById(R.id.username);
holder.date = (TextView) convertView.findViewById(R.id.dateValue);
holder.time = (TextView) convertView.findViewById(R.id.timeValue);
holder.notType = (TextView) convertView.findViewById(R.id.notType);
holder.newMsg = (ImageView) convertView.findViewById(R.id.newMsg);
holder.realUsername = (TextView) convertView.findViewById(R.id.realUsername);
holder.replied = (ImageView) convertView.findViewById(R.id.replied);
holder.msgID = (TextView) convertView.findViewById(R.id.msgID_fr);
holder.avatar = (ImageView) convertView.findViewById(R.id.buddyIcon);
holder.msgPreview = (TextView) convertView.findViewById(R.id.msgPreview);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
and the image is getting loaded this way:
Thread sepThread = new Thread() {
public void run() {
String ava;
ava = request[8].replace(".", "_micro.");
Log.e("ava thread",ava+", username: "+request[0]);
avatar = getImageBitmap(URL+ava);
buddyIcon = holder.avatar;
mHandler.post(mUpdateResults);
//holder.avatar.setImageBitmap(getImageBitmap(URL+ava));
}
};
sepThread.start();
Now, the problem I'm having is that if there are more items that need to display the same picture, not all of those pictures get displayed. When you scroll up and down the list maybe you end up filling all of them.
When I tried the commented out line (holder.avatar.setImageBitmap...) the app sometimes force closes with "only the thread that created the view can request...". But only sometimes.
Any idea how I can fix this? Either option.
© Stack Overflow or respective owner