我的代码中的适配器如下,我扩展了基础适配器:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder vHolder;
// if (convertView == null) {
vHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.home_item, null);
vHolder.albumIcon = (ImageView) convertView
.findViewById(R.id.albumIcon);
try {
Bitmap icon = aws.getAlbumImg(itemInfolist.get(position)
.getAlbumInfoCol().get(0).getAlbumID(), 0);
if (icon != null) {
vHolder.albumIcon.setImageBitmap(icon);
} else {
vHolder.albumIcon.setImageBitmap(BitmapFactory.decodeResource(
context.getResources(), R.drawable.album));
}
} catch (Exception e) {
vHolder.albumIcon.setImageBitmap(BitmapFactory.decodeResource(
context.getResources(), R.drawable.album));
}
convertView.setTag(vHolder);
return convertView;
}
但是,我异步下载了想象,
当调用 Bitmap icon = aws.getAlbumImg(itemInfolist.get(position).getAlbumInfoCol().get(0).getAlbumID(), 0);
一些没有下载的图片会使用默认图片,这些图片下载到另一个Web Service Bean后,我希望Web Service bean发送消息调用这个适配器中的getView方法,以实现自动刷新功能。
但是如果我如下更改Web Service Download Bean,就会导致异常
03-19 07:46:33.241: 错误/AndroidRuntime(716): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。
HomeAdapter mHomeAdapter;
public AlbumWS(HomeAdapter homeAdapter) {
mHomeAdapter = homeAdapter;
}
And after download,
public boolean getAlbumImgWS(final ArrayList<Integer> albumIDs) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
AlbumInfoWS aiws = new AlbumInfoWS();
for (int i = 0; i < albumIDs.size(); ++i) {
if (ABSCENTALBUMIMGS.contains(albumIDs.get(i))) {
continue;
}
if (FunctionUtil.isExist(albumIDs.get(i))) {
continue;
}
String urlPath = aiws.getAlbumImage("en_US",
Config.IMG_ATTIBUTETYPE, albumIDs.get(i));
boolean ret = FunctionUtil.simpleDownload(Config.HOST
+ urlPath, "/data/data/com.greysh.amped/img/"
+ albumIDs.get(i) + ".jpg");
if (!ret) {
if (!ABSCENTALBUMIMGS.contains(albumIDs.get(i))) {
ABSCENTALBUMIMGS.add(albumIDs.get(i));
}
}
mHomeAdapter.notifyDataSetChanged();
}
}
}).start();
return true;
}