Reputation: 65
I am trying to create a gallery app for Android and I am using Volley and Glide.
I have an ImageAdapter class, which looks like this
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class ImageAdapter extends BaseAdapter {
private Context CTX;
private Integer image_id[] = {R.mipmap.sample_0,
R.mipmap.sample_1, R.mipmap.sample_2,
R.mipmap.sample_3,
R.mipmap.sample_4,
R.mipmap.sample_5,
R.mipmap.sample_6,
R.mipmap.sample_7
};
public ImageAdapter(Context CTX) {
this.CTX = CTX;
}
@Override
public int getCount() {
return image_id.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView img;
if (arg1 == null) {
img = new ImageView(CTX);
img.setLayoutParams(new GridView.LayoutParams(200, 200));
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
img.setPadding(8, 8, 8, 8);
} else {
img = (ImageView) arg1;
}
Glide.with(CTX)
.load(image_id[arg0])
.placeholder(R.mipmap.warning)
.centerCrop()
.crossFade()
.into(img);
return img;
}
}
Right now I am passing to the adapter images from the image_id array, but in the MainActivity I am making a volley request which gets me some images(urls) from my server, which I store in a String array.
How can I pass that array of String URLs to the ImageAdapter class?
Upvotes: 0
Views: 115
Reputation: 3401
pass ArrayList
of images to the adapter in the constructor like
private Context cxt;
private ArrayList<String> images;
public ImageAdapter(Context cxt,ArrayList<String> images){
this.cxt = cxt;
this.images = images;
}
@override
public void getCount(){
return images == null ? 0 : images.size();
}
initially you can pass an empty arraylist
and after loading data from volley updated the arraylist
and call adapter.notifyDataSetChanged()
and in your getview you can access the image from the arraylist using position
Upvotes: 1
Reputation: 962
Just make some modification in Adapter:
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class ImageAdapter extends BaseAdapter {
private Context CTX;
private int image_id[] = {R.mipmap.sample_0,
R.mipmap.sample_1, R.mipmap.sample_2,
R.mipmap.sample_3,
R.mipmap.sample_4,
R.mipmap.sample_5,
R.mipmap.sample_6,
R.mipmap.sample_7
};
private String image_urls[];
public ImageAdapter(Context CTX) {
this.CTX = CTX;
}
@Override
public int getCount() {
if (image_urls != null && image_urls.length > 0) return image_urls.length;
return image_id.length;
}
public void updateData(String[] urls) {
this.image_urls = urls;
this.notifyDataSetChanged();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView img;
if (arg1 == null) {
img = new ImageView(CTX);
img.setLayoutParams(new GridView.LayoutParams(200, 200));
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
img.setPadding(8, 8, 8, 8);
} else {
img = (ImageView) arg1;
}
if (image_urls != null && image_urls.length > arg0) {
Glide.with(CTX)
.load(image_urls[arg0])
.placeholder(R.mipmap.warning)
.centerCrop()
.crossFade()
.into(img);
} else {
Glide.with(CTX)
.load(image_id[arg0])
.placeholder(R.mipmap.warning)
.centerCrop()
.crossFade()
.into(img);
}
return img;
}
}
}
And when Volley returns successful:
yourAdapter.updateData(yourStringUrlArray);
Upvotes: 0