A
A
Alexander2014-05-19 08:03:12
Android
Alexander, 2014-05-19 08:03:12

Android: Simple example of loading images into GridView using Android-Universal-Image-Loader?

Hello!
There is a list of images url, activity with GridView, Android-Universal-Image-Loader library.
Please help me to give a simple example of loading images into GridView using Android-Universal-Image-Loader.
On the github itself, there is not a very simple example on this issue ( https://github.com/nostra13/Android-Universal-Imag...
Thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2014-05-19
@cry_san

Like this:
1. Add a GridView to your application.
2. Make your own custom adapter to it. For example, like this:

class MyGridAdapter extends BaseAdapter {
  
  Context context;
  ArrayList<String> items;
  
  MyGridAdapter(Context context, ArrayList<String> items) {
    this.context=context;
    this.items=items;
  }

  @Override
  public int getCount() {
    return items.size();
  }

  @Override
  public Object getItem(int arg0) {
    return items.get(arg0);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
      imageView = new ImageView(context);
      imageView.setLayoutParams(new GridView.LayoutParams(93, 93));
      imageView.setScaleType(ImageView.ScaleType.CENTER);
    } else imageView=(ImageView) convertView;
    
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    DisplayImageOptions options = new DisplayImageOptions.Builder()
                                           .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                                           .cacheInMemory()
                                           .cacheOnDisc()
                                           .build();
    imageLoader.displayImage(items.get(position), imageView, options);
    
    return imageView;
  }
  
}

3. Install this adapter in your GridView:
ArrayList<String> items = new ArrayList<String>();
MyGridAdapter adapter = new MyGridAdapter(this, items);
gridView.setAdapter(adapter);

items in this case should contain a list of image URLs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question