0
private Context mContext;
private int[] colors = new int[] { Color.WHITE, 0x30aaaaaa };
private int[] dotColors = new int[7];
private List<Integer> sta;
private TasksDataSource datasource;
private int con=1000;
public Integer[] mThumbIds = {
        R.drawable.green, R.drawable.green,
        R.drawable.green, R.drawable.green,
        R.drawable.green, R.drawable.green,
        R.drawable.green, R.drawable.green,
        R.drawable.green, R.drawable.green,
        R.drawable.green, R.drawable.green,
        R.drawable.green, R.drawable.green,
        R.drawable.green
    };
public ImageAdapter(Context context, List<Integer> content){
sta = content;
datasource = new TasksDataSource(context); //here
datasource.open(); 
mContext=context;
}
@Override
public int getCount() {
    return mThumbIds.length;
}
@Override
public Object getItem(int position) {
    return mThumbIds[position];
}
@Override
public long getItemId(int position) {
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int[] st = new int[sta.size()];   
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(20, 20));
    return imageView;
}

I had a loop for dynamic intialisation which has some alterations in the previous code..

public Integer[] mThumbIds = new Integer[con];
for(int i = 0;i < st.length;i++)
      {
        st[i] = sta.get(i);
        Log.i("st::" + st[i]," ");

      }
      int ii=0,jj=0;
      while(ii<con)
      {
          if(st[jj]==0)
          {
              mThumbIds[ii]=R.drawable.red;
          }
          else if(st[jj]==1)
          {
              mThumbIds[ii]=R.drawable.green;
          }
          else
          {
              mThumbIds[ii]=R.drawable.grey;
          }
      }

the above addition din't work and runs infinitely and gets stopped. What I want is, display red img wen mThumbIds[ii] is 0, green when mThumbIds[ii] is 1, grey when mThumbIds[ii] is 2. how can I achieve this??

AnRu
  • 67
  • 8

1 Answers1

0

Use a Universal Image Loader.

https://github.com/nostra13/Android-Universal-Image-Loader.

It is based on Lazy List(works on same principle). Can load images locally or form a server. But it has lot of other configurations. I would prefer to use Universal Image Loader coz it gives you more configuration options. You can display a error image if downlaod failed. Can display images with rounded corners. Can cache on disc or memory. Can compress image.

In your custom adapter constructor

 public Integer[] mThumbIds = {
    R.drawable.red, R.drawable.green,
    R.drawable.grey, .....
   };
 File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

 // Get singletone instance of ImageLoader
 imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
      // You can pass your own memory cache implementation
     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
     .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
     .enableLogging()
     .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
 .showStubImage(R.drawable.stub_id)//display stub image
 .cacheInMemory()
 .cacheOnDisc()
 .displayer(new RoundedBitmapDisplayer(20))
 .build();

In your getView()

According to the postition display images.

 ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
 imageLoader.displayImage(mThumbIds[position], image,options);//provide imageurl, imageview and options.

You can configure with other options to suit your needs.

Along with Universal Image Loader you can view holder for smooth scrolling and performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html .

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • How do you use displayImage(mThumbIds[position]) when mThumbIds is an Integer and it asks for a String. I'm getting an error when trying that. – RED_ Oct 25 '13 at 17:04
  • @RED_ complete sample loading from url http://stackoverflow.com/questions/16789676/caching-images-and-displaying – Raghunandan Oct 25 '13 at 17:44
  • That's URLs though, I'm using a tonne of local drawables in an int. It's telling me that there should be a string there. I'll look back at the tutorials – RED_ Oct 25 '13 at 17:50
  • @RED_ use a method that suits your need. check the available methods – Raghunandan Oct 25 '13 at 17:51