The adapter in my code as follows, I extends the base adapter:
@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;
}
However, I download the imagine asynchronously,
When invoke Bitmap icon = aws.getAlbumImg(itemInfolis开发者_如何学运维t.get(position).getAlbumInfoCol().get(0).getAlbumID(), 0);
Some pictures which haven't downloaded will use the default image, after these picutures have downloaded in another Web Service Bean, I want the Web Service bean sends a message to invoke the getView method in this adapter in order to implement the auto refresh function.
But if I change the Web Service Download Bean as follows,it will cause the exception
03-19 07:46:33.241: ERROR/AndroidRuntime(716): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
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;
}
android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
This exception means that you are trying to update the UI from another thread - in Android this is not permitted.
You should use a Handler
to post the changes to the main UI thread.
Instead of
new Runnable() {
public void run() {
...
}
};
try
mHandler.post(new Runnable() {
public void run() {
...
}
};);
精彩评论