安卓相册视图如何更新图片

3

我有一个关于使用GalleryView的问题。

首先,我设置了五个“默认图片”从drawable目录中显示。

但是之后,我想要运行一个异步任务,在其中下载图片,保存它们并在画廊中显示它们。

为此,我创建了以下适配器:

    public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private ArrayList<Integer> mImageIds = new ArrayList<Integer>();
    private ArrayList<Drawable> mImageDrawables = new ArrayList<Drawable>();

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }

    public void setPlaces(int count) {
        for (int i = 0; i < count; i++) {
            mImageIds.add(R.drawable.tournoimg);
            mImageDrawables.add(null);
        }
    }

    public void setDrawable(String resource, int position) {
        Drawable image = Drawable.createFromPath(resource);
        mImageDrawables.add(position, image);
    }

    public int getCount() {
        return mImageIds.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        if (mImageDrawables.get(position) == null)
            i.setImageResource(mImageIds.get(position));
        else
            i.setImageDrawable(mImageDrawables.get(position));
        i.setLayoutParams(new Gallery.LayoutParams(60, 78));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);
        return i;
    }
}

}

还有以下的异步任务

    private class FillImages extends AsyncTask<ArrayList<Place>, Void, Void> {

    protected Void doInBackground(ArrayList<Place>... listplaces) {
        ArrayList<Place> places = listplaces[0];
        Iterator<Place> it = places.iterator();
        int position = 0;
        while (it.hasNext()) {
            Place p = it.next();
            saveImage(p.getImage(), p.getURLImage());
            // Gallery g = (Gallery) findViewById(R.id.gallery);
            mImageAdapter.setDrawable(p.getImage(), position);
            position++;
            mImageAdapter.notifyDataSetChanged();
        }
        return (null);
    }

但是当我运行它时,出现了以下错误:

Caused by: android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。

有什么想法吗?

谢谢


我不太理解已接受的解决方案。你能否编辑并添加你的更改来解决这个问题?这对许多人来说可能非常有帮助。谢谢。 - ferguior
2个回答

3

您应该实现onPostExecute()方法,并将任何与UI交互的代码(例如notifyDataSetChange())移动到该方法中。Android要求与GUI的交互必须在UI线程中进行。


1

为此,您需要将此代码放入Async任务的onPostExecute()方法中:

 ArrayList<Place> places = listplaces[0];
    Iterator<Place> it = places.iterator();
    int position = 0;
    while (it.hasNext()) {
        Place p = it.next();
        saveImage(p.getImage(), p.getURLImage());
        // Gallery g = (Gallery) findViewById(R.id.gallery);
        mImageAdapter.setDrawable(p.getImage(), position);
        position++;
        mImageAdapter.notifyDataSetChanged();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接