我在ListActivity中有一个扩展的BaseAdapter:
private static class RequestAdapter extends BaseAdapter {
其中定义了一些处理程序和runnable
// 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);
}
在适配器的getview函数中,我得到如下视图:
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();
}
图像是这样加载的:
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();
现在,我遇到的问题是,如果有更多的项目需要显示同一张图片,并不是所有这些图片都会显示出来。当你在列表中上下滚动时,也许你最终会填满所有的列表。
当我尝试注释行(holder.avatar.setimagebitmap…)时,应用程序有时会强制关闭“只有创建视图的线程可以请求…”。但只是有时候。
你知道我该怎么解决吗?任何一种选择。
最佳答案
当调用getview时,您得到了一个avatar imageview。您应该将此实例传递给septhread,septhread应该将此实例传递给mupdateresults。这样,位图将精确显示到下载它的imageview。否则位图将显示给某些不正确的BudddyIcon实例。您的线程下载了一段时间的图像,当它准备好时,BuddyIcon引用了另一个ImageView,因为已经调用了另一个GetView。
应该是这样的:
public View getView(...){
//skip
(new SepThread(holder.avatar)).start();
}
public class SepThread extends Thread() {
ImageView imageView;
public SepThread(ImageVIew iv){
imageView=iv;
}
public void run() {
//skip
Bitmap avatar = getImageBitmap(URL+ava);
mHandler.post(new UpdateResults(imageView, avatar));
}
};
class UpdateResults extends Runnable() {
ImageView imageView;
Bitmap bitmap;
public UpdateResults(ImageView iv, Bitmap b){
imageView=iv;
bitmap=b;
}
public void run() {
loadAvatar(imageView, bitmap);
}
};
protected static void loadAvatar(ImageView iv, Bitmap b) {
iv.setImageBitmap(b);
}
当然,正如disretrospect上面所说的,你应该意识到回收的转换视图。
我做了一个完整的懒散的例子并发布了源码,可能也有帮助Lazy load of images in ListView。