我在 ListActivity 中有一个扩展的 BaseAdapter:
private static class RequestAdapter extends BaseAdapter {
以及其中定义的一些处理程序和可运行对象
// 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 ...)时,应用程序有时会强制关闭“只有创建视图的线程才能请求......”。但只是有时。
知道如何解决这个问题吗?任一种选择。