我想下载并在gridView适配器中显示一些图像。这是我的适配器
public class CustomAdapter extends BaseAdapter {
private Context context;
ArrayList<String> list = null;
public CustomAdapter (Context context, ArrayList<String> list) {
this.context = context;
this.list= list;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
public int getCount() {
return list.size();
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItem(int)
*/
public Object getItem(int paramInt) {
return paramInt;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId(int paramInt) {
return paramInt;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View child, ViewGroup parent) {
//Inflating View
.....................
GridItem gridItem = new GridItem (imageview);
DownloadContent downloadContent = new DownloadContent();
downloadContent.setDownloadUrl(list.get(0));
downloadContent.setContentType(DownloadContent.CONTENT_TYPE_IMAGE);
downloadContent.setDownloadFilePath(filepath);
downloadContent.setDownloadNotifier(imageDownloadNotifier);
downloadContent.setTag(gridItem);
new DownloadTask(new DownloadContent[] { downloadContent }).execute();
//
return view;
}
private DownloadNotifier imageDownloadNotifier = new DownloadNotifier() {
@Override
public void downloadCompleted(DownloadContent downloadContent) {
if(downloadContent.isSuccess())
{
GridItem gridItem = (GridItem) downloadContent.getTag();
ImageView imgView = mgridItem.getImage();
Bitmap bitmap = BitmapFactory.decodeFile(downloadContent.getDownloadFilePath());
imgView.setImageBitmap(bitmap);
}
}
};
}
这显示以下异常
-26 11:27:33.629: E/AndroidRuntime(2082): FATAL EXCEPTION: Thread-24
10-26 11:27:33.629: E/AndroidRuntime(2082): android.view.ViewRoot$CalledFromWrongThreadException: **Only the original thread that created a view hierarchy can touch its views.**
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.ViewRoot.checkThread(ViewRoot.java:3020)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.ViewRoot.requestLayout(ViewRoot.java:634)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.View.requestLayout(View.java:8267)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.View.requestLayout(View.java:8267)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.View.requestLayout(View.java:8267)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.View.requestLayout(View.java:8267)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.widget.AbsListView.requestLayout(AbsListView.java:1157)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.View.requestLayout(View.java:8267)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.View.requestLayout(View.java:8267)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.view.View.requestLayout(View.java:8267)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.widget.ImageView.setImageDrawable(ImageView.java:322)
10-26 11:27:33.629: E/AndroidRuntime(2082): at android.widget.ImageView.setImageBitmap(ImageView.java:336)
我该如何解决这个问题?如何更新有关下载图像的视图?
先谢谢
答案 0 :(得分:0)
android.view.ViewRoot $ CalledFromWrongThreadException:只有 创建视图层次结构的原始线程可以触及其视图。
这是问题所在,因为您正试图直接从主线程内部从Web获取图像。相反,您应该实现异步图像加载逻辑。
我建议你检查并实现如下的逻辑:Universal Image loader