我有关于使用问题图库视图。

首先,我设置五“默认图像”,以从可绘目录显示。

不过,以后我要运行一个异步任务中,我下载图像,保存并展示他们在画廊。

有关,我创建了以下适配器:

    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);
    }

但是,当我运行它,我有此错误:  android.view.ViewRoot $ CalledFromWrongThreadException:通过引起的。只有创建视图层次可以触摸其意见原始线程

任何想法?

由于

有帮助吗?

解决方案

您应该实现onPostExecute()并移动与UI(也许notifyDataSetChange())相互作用的任何代码,以该方法。机器人需要与GUI交互从UI线程发生。

其他提示

有关,你必须把这个代码为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();
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top