Pregunta

Tengo una pregunta sobre el uso de GalleryView.

Al principio, configuré cinco "imágenes predeterminadas" para que se mostraran desde el directorio dibujable.

Pero después, quiero ejecutar una tarea asíncrona en la que descargo las imágenes, las guardo y las muestro en la galería.

Para eso creé el siguiente Adaptador:

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

}

y la siguiente tarea asíncrona

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

Pero cuando lo ejecuto me sale este error:Causado por:android.view.ViewRoot$CalledFromWrongThreadException:Sólo el hilo original que creó una jerarquía de vistas puede tocar sus vistas.

¿Alguna idea?

Gracias

¿Fue útil?

Solución

Debe implementar onPostExecute() y mover cualquier código que interactúa con la interfaz de usuario (quizás notifyDataSetChange()) a ese método. Android requiere que la interacción con la GUI ocurra desde el hilo de interfaz de usuario.

Otros consejos

Para que usted tiene que poner este código en el método de trabajo asíncrono 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();
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top