3

i have a question about using GalleryView.

At first, i set five "default images" to show from drawable directory.

But after, i want to run an Async Task in which i download the images, save them and show them in the gallery.

For that i created the following Adapter:

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

}

and the following Async Task

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

But when i run it i have this error: Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Any idea?

Thanks

Gerardo
  • 5,800
  • 11
  • 66
  • 94
  • I don't quite understand the accepted solution. Can you edit and add your changes to solve the issue? It can be very helpful for many people. Thanks. – ferguior Nov 15 '11 at 18:13

2 Answers2

3

You should implement onPostExecute() and move any code that interacts with the UI (perhaps notifyDataSetChange()) to that method. Android requires that interaction with the GUI happen from the UI thread.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Jay Askren
  • 10,282
  • 14
  • 53
  • 75
1

For that you have to put this code into onPostExecute() method of Async task :

 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();
    }
Mitul Goti
  • 2,657
  • 1
  • 22
  • 19