package com.ternaryop.photoshelf.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ternaryop.lazyimageloader.ImageLoader;
import com.ternaryop.photoshelf.ImageInfo;
import com.ternaryop.photoshelf.R;

public class ImagePickerAdapter extends ArrayAdapter<ImageInfo> implements View.OnClickListener {
    private ImageLoader imageLoader;
	private final LayoutInflater inflater;
	private OnPhotoPickerClick onPhotoPickerClick;

	public ImagePickerAdapter(Context context) {
		super(context, 0);
        imageLoader = new ImageLoader(context.getApplicationContext(), "picker", R.drawable.stub);
		inflater = LayoutInflater.from(context);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.gridview_photo_picker_item, parent, false);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (onPhotoPickerClick != null) {
			holder.caption.setOnClickListener(this);
			holder.caption.setTag(position);
		}

		ImageInfo imageInfo = getItem(position);
		ViewGroup.LayoutParams imageLayoutParams = holder.thumbImage.getLayoutParams();
//		int minThumbnainWidth = 140;
//		int minThumbnainHeight = 140;
//		// convert from pixel to DIP
//		imageLayoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minThumbnainWidth, getContext().getResources().getDisplayMetrics());
//		imageLayoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minThumbnainHeight, getContext().getResources().getDisplayMetrics());
		imageLoader.displayImage(imageInfo.getThumbnailURL(), holder.thumbImage);

		return convertView;
	}

	public void onClick(final View v) {
		switch (v.getId()) {
			case R.id.caption:
				onPhotoPickerClick.viewClick((Integer) v.getTag());
				break;
		}
	}

	@Override
	public boolean isEnabled(int position) {
		return getItem(position).getSelector() != null;
	}

	private class ViewHolder {
		final TextView caption;
		final ImageView thumbImage;

		public ViewHolder(View vi) {
			caption = (TextView)vi.findViewById(R.id.caption);
			thumbImage = (ImageView)vi.findViewById(R.id.thumbnail_image);
		}
	}

	public void setOnPhotoPickerClick(OnPhotoPickerClick onPhotoPickerClick) {
		this.onPhotoPickerClick = onPhotoPickerClick;
	}

	public interface OnPhotoPickerClick {
		public void viewClick(int position);
	}
}
@@ -36,16 +36,68 @@
 apter;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import a
@@ -235,21 +235,20 @@
 .widget.
-Array
+Base
 Adapter;
@@ -487,31 +487,19 @@
 nds 
-Array
+Base
 Adapter
-%3CImageInfo%3E
  imp
@@ -541,16 +541,22 @@
 private 
+final 
 ImageLoa
@@ -659,16 +659,53 @@
 erClick;
+%0A%09private ArrayList%3CImageInfo%3E items;
 %0A%0A%09publi
@@ -748,29 +748,8 @@
 ) %7B%0A
-%09%09super(context, 0);%0A
     
@@ -887,18 +887,58 @@
 text);%0A%09
-%7D%0A
+%09items = new ArrayList%3C%3E();%0A%09%7D%0A%0A%09@Override
 %0A%09public
@@ -1445,607 +1445,503 @@
 ;%0A%09%09
-ViewGroup.LayoutParams imageLayoutParams = holder.thumbImage.getLayoutParams();%0A//%09%09int minThumbnainWidth = 140;%0A//%09%09int minThumbnainHeight = 140;%0A//%09%09// convert from pixel to DIP%0A//%09%09imageLayoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minThumbnainWidth, getContext().getResources().getDisplayMetrics());%0A//%09%09imageLayoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minThumbnainHeight, getContext().getResources().getDisplayMetrics());%0A%09%09imageLoader.displayImage(imageInfo.getThumbnailURL(), holder.thumbImage);%0A%0A%09%09return convertView
+imageLoader.displayImage(imageInfo.getThumbnailURL(), holder.thumbImage);%0A%0A%09%09return convertView;%0A%09%7D%0A%0A%09@Override%0A%09public int getCount() %7B%0A%09%09return items.size();%0A%09%7D%0A%0A%09@Override%0A%09public ImageInfo getItem(int position) %7B%0A%09%09return items.get(position);%0A%09%7D%0A%0A%09@Override%0A%09public long getItemId(int position) %7B%0A%09%09return position;%0A%09%7D%0A%0A%09@Override%0A%09public boolean isEnabled(int position) %7B%0A%09%09return getItem(position).getSelector() != null;%0A%09%7D%0A%0A%09public void addAll(List%3CImageInfo%3E list) %7B%0A%09%09items.addAll(list)
 ;%0A%09%7D
@@ -2103,115 +2103,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public boolean isEnabled(int position) %7B%0A%09%09return getItem(position).getSelector() != null;%0A%09%7D%0A%0A
 %09pri

Use BaseAdapter