Pergunta

Eu tenho uma pergunta sobre o uso da GalleryView.

No começo, defina cinco "imagens padrão" para mostrar do diretório desenhado.

Mas depois, quero executar uma tarefa assíncrona na qual baixe as imagens, salve -as e mostro -as na galeria.

Para isso, criei o seguinte 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;
    }
}

}

e a seguinte tarefa assí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);
    }

Mas quando eu o executo, tenho o erro: causado por: Android.View.Viewroot $ chamadoFromWrongThreadException: Somente o thread original que criou uma hierarquia de visualizações pode tocar em suas visualizações.

Qualquer ideia?

Obrigado

Foi útil?

Solução

Você deve implementar onPostExecute() e mover qualquer código que interaja com a interface do usuário (talvez notifyDataSetChange()) para esse método. O Android exige que a interação com a GUI aconteça no thread da interface do usuário.

Outras dicas

Para isso, você deve colocar este código no método onPostExecute () da tarefa assíncrona:

 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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top