0

I have a GridView that im using to build a gallery, but im having this problem that the images are not been showing on the first time i call the Gallery Activity, but if i press BackButton and then open again the Activity, the gridview show the images (if a rotate the screen, images are show too). I'm getting the images downloaded from URLs.

Here is the code of Gallery:

public class Galeria extends ActionBarActivity {

    private AdapterGaleria adapter;
    private ArrayList<Foto> fotos;
    private GridView grid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_galeria);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));
        getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.icon));

        Bundle b = getIntent().getExtras();

        final Fotos fts = (Fotos)b.getSerializable("photos");

        fotos = ((Fotos)b.getSerializable("photos")).getFotos();
        grid = (GridView)findViewById(R.id.grid_galeria);
        adapter = new AdapterGaleria(getApplicationContext(), R.layout.item_galeria, fotos);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Intent it = new Intent(Galeria.this, GaleriaAberta.class);
                it.putExtra("position", position);
                it.putExtra("photos", fts);
                startActivity(it);

            }
        });
    }
}

And this is the code of adapter:

public class AdapterGaleria extends ArrayAdapter<Foto>{

    private LayoutInflater inflater;
    private Context context;
    private ArrayList<Foto> imagesToLoad;

    public AdapterGaleria(Context context, int resource, List<Foto> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        this.imagesToLoad = (ArrayList<Foto>)objects;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imagesToLoad.size();
    }

    @Override
    public Foto getItem(int position) {
        // TODO Auto-generated method stub
        return imagesToLoad.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class ViewHolder{

        ImageView image;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("GET_VIEW", "get_view");
        Foto f = new Foto();
        f = getItem(position);
        ViewHolder holder;
        View v;
        v = convertView;
        if (v == null){
            holder = new ViewHolder();
            v = inflater.inflate(R.layout.item_galeria, parent, false);
            holder.image = (ImageView)v.findViewById(R.id.image_galeria);
            v.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        AQuery aq = new AQuery(context);
        aq.id(holder.image).image(imagesToLoad.get(position).getNormal(), true, false, 300, 0, null,0,1.0f / 1.0f);

        return v;
    }

}
WitaloBenicio
  • 3,395
  • 5
  • 25
  • 32
  • is the `onCreate()` method called that first time that it does not display? same for `getView()`? – Al Lelopath Jul 22 '14 at 13:40
  • Yep, both methods are called. If i just rotate the screen, or press back and open gallery again, it works. I think that should be something on the async download. – WitaloBenicio Jul 22 '14 at 13:43
  • Yep, both methods are called. If i just rotate the screen, or press back and open gallery again, it works. I think that should be something on the async download. – WitaloBenicio Jul 22 '14 at 13:45
  • Hello Witalo, did you get any solution to this please? I know it has been long time, but today I switched from SDK 28 to 32 and am facing same issue. The same code has been running for the past decade for me, but not anymore. Just like you, going to another activity and coming back, makes it appear. – nirav dinmali Oct 15 '22 at 09:12
  • I just need to pass a preload image to the view before the AQuery complete the download, and then when the download is done, the images are showed in the gridview. :) – WitaloBenicio Oct 15 '22 at 13:11

2 Answers2

0

In your Adapter use the resource id, you get from the constructor and check whether it works

public class AdapterGaleria extends ArrayAdapter<Foto>{

    private LayoutInflater inflater;
    private Context context;
    private ArrayList<Foto> imagesToLoad;
    private int resource;

    public AdapterGaleria(Context context, int resource, List<Foto> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        this.imagesToLoad = (ArrayList<Foto>)objects;
        this.context = context;
        this.resource = resource;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imagesToLoad.size();
    }

    @Override
    public Foto getItem(int position) {
        // TODO Auto-generated method stub
        return imagesToLoad.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class ViewHolder{

        ImageView image;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("GET_VIEW", "get_view");
        Foto f = new Foto();
        f = getItem(position);
        ViewHolder holder;
        View v;
        v = convertView;
        if (v == null){
            holder = new ViewHolder();
            v = inflater.inflate(resource, parent, false);
            holder.image = (ImageView)v.findViewById(R.id.image_galeria);
            v.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        AQuery aq = new AQuery(context);
        aq.id(holder.image).image(imagesToLoad.get(position).getNormal(), true, false, 300, 0, null,0,1.0f / 1.0f);

        return v;
    }

}
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Sweety Bertilla
  • 972
  • 10
  • 35
0

I just need to pass a preload image to the view before the AQuery complete the download, and then when the download is done, the images are showed in the gridview. :)

WitaloBenicio
  • 3,395
  • 5
  • 25
  • 32