如何从另一个WebService Bean调用Android中baseAdapter中的getView方法?
我的代码中的适配器如下,我扩展了基本适配器:
@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: ERROR/AndroidRuntime(716): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that create视图层次结构可以触及其视图。
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;
}
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(itemInfolist.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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此异常意味着您正在尝试从另一个线程更新 UI - 在 Android 中这是不允许的。
您应该使用
Handler
将更改发布到主 UI 线程。而不是
尝试
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
try