package com.oux.photocaption;

import java.util.ArrayList;
import java.io.IOException;

import android.util.Log;
import android.app.Activity;
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 android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Toast;
import com.oux.loader.ImageLoader;
import com.oux.loader.ViewHolder;

/**
 *
 * @author javatechig {@link http://javatechig.com}
 *
 */
public class GridViewAdapter extends ArrayAdapter {
    private Context mContext;
    private int layoutResourceId;
    private Cursor externalCursor;
    private Uri externalContentUri;
    private int externalColumnIndex;
    static final String TAG = "photoCaptionGridViewAdapter";
    private ImageLoader mLoader;

    public GridViewAdapter(Context context, int layoutResourceId) {
        super(context, layoutResourceId);
        this.layoutResourceId = layoutResourceId;
        this.mContext = context;
		mLoader = new ImageLoader(context);
        //Do the query
        externalContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;        

        String[] projection = {MediaStore.Images.Media._ID}; 
        String selection = "";
        String [] selectionArgs = null;
        externalCursor = mContext.getContentResolver().query(
            externalContentUri,projection,selection,selectionArgs,null); 
        externalColumnIndex = externalCursor.getColumnIndex(MediaStore.Images.Media._ID);
    }


    public Uri getUri(int position) {
        externalCursor.moveToPosition(getCount()-(position+1));
        int imageID = externalCursor.getInt( externalColumnIndex );
        return  Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,Integer.toString(imageID));
    }

    public void clearCache() {
        mLoader.clearCache();
    }

    @Override
    public void notifyDataSetChanged() {
        mLoader.clearCache();
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.caption_preview);
            holder.image = (ImageView) row.findViewById(R.id.image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
		mLoader.DisplayImage(getCount()-(position+1), holder);
        return row;
    }

    public int getCount() {
        return externalCursor.getCount();
    }
}


@@ -1200,16 +1200,17 @@
 ntext);%0A
+%0A
         
@@ -1215,14 +1215,21 @@
   //
-Do the
+ Make a first
  que
@@ -1309,17 +1309,8 @@
 URI;
-        %0A
 %0A   
@@ -1366,17 +1366,16 @@
 ia._ID%7D;
- 
 %0A       
@@ -1572,17 +1572,16 @@
 s,null);
- 
 %0A       
@@ -1966,126 +1966,561 @@
     
-public void clearCache() %7B%0A        mLoader.clearCache();%0A    %7D%0A%0A    @Override%0A    public void notifyDataSetChanged() %7B
+@Override%0A    public void notifyDataSetChanged() %7B%0A        // Make again the query to take into account adds and deletes%0A        externalContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;%0A        String%5B%5D projection = %7BMediaStore.Images.Media._ID%7D;%0A        String selection = %22%22;%0A        String %5B%5D selectionArgs = null;%0A        externalCursor = mContext.getContentResolver().query(%0A            externalContentUri,projection,selection,selectionArgs,null);%0A        externalColumnIndex = externalCursor.getColumnIndex(MediaStore.Images.Media._ID);%0A
 %0A   

Fix shift issue when delete or add image (remake query on DataSetChanged