I am new to android and I'm trying to figure out how to create a gallery of ImageViews on an app. I have a server running that serves the images to the app which download correctly into a Arraylist of ImageViews. I just need to add all of these images to a grid view. I know I need to use an adaptor but I don't know how.
This is the code I have in my activity (images is populated else where)
ArrayList<ImageView> images = new ArrayList<>();
GridView table = (GridView) findViewById(R.id.table);
table.setAdapter(new ImageAdaptor(this.getBaseContext(), images));
And this is my adaptor
public class ImageAdaptor extends BaseAdapter{
private Context context;
private ArrayList<ImageView> images;
public ImageAdaptor(Context context, ArrayList<ImageView> images){
this.context = context;
this.images = images;
}
public int getCount(){
return this.images.size();
}
public Object getItem(int position){
return this.images.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView image;
if (convertView == null){
image = new ImageView(this.context);
image.setLayoutParams(new GridView.LayoutParams(115, 115));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
}else{
image = (ImageView) convertView;
}
return image;
}
}
First thing, you should not make the arraylist of views as it wont be efficient for reusing views. However if you still want to do that, then solution is ,
public View getView(int position, View convertView, ViewGroup parent){
return images.get(position);
}