0

i have an activity that include imageview and gridview.the rules is when i click selected image on gridview,it will be previewed by imageview.then,the imageview can be clicked and it will be showed on dialog.the problem is when i clicked the imageview, the image is not match with selected image in gridview.here my code.

public class GalleryActivity extends Activity {

ImageView imgviewThumb, imgviewPreviewDialog;
public int[] imageResto = {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4
};
int position ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);

    imgviewThumb = (ImageView)findViewById(R.id.imgview_preview);
    imgviewThumb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Dialog dialog = new Dialog(GalleryActivity.this);
            dialog.setCancelable(true);
            dialog.setContentView(R.layout.dialog_previewimage);
            imgviewPreviewDialog = (ImageView) dialog.findViewById(R.id.imgview_preview_dialog);
            imgviewPreviewDialog.setImageResource(imageResto[position]);
            dialog.show();
        }
    });

    GridView gView = (GridView) findViewById(R.id.grid_View);

    //Instantiating ImageAdapter class
    gView.setAdapter(new ImageAdapter(this));

    /*showing full image on clicking the thumbnail
     * create a new class named FullImage.java 
     */
    gView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            imgviewThumb.setImageResource(imageResto[position]);
        }
    });
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);

        return imageView;
    }

    private Context mContext;

    // images in array
    public Integer[] mThumbIds = {
             R.drawable.thumb1_e,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4,
             R.drawable.thumb5,
             R.drawable.thumb1,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4
    };
}
}

please correct me.thanks for advance.

2
  • 1
    since you have an Array, why not extending ArrayAdapter instead of BaseAdapter? Commented Sep 11, 2012 at 8:05
  • @Remdroid the logcat shows that there are java.lang.ArrayIndexOutOfBoundsException: length=9; index=9.thanks for suggest :-) Commented Sep 12, 2012 at 2:27

2 Answers 2

1

Try changing:

public Object getItem(int position) {
        return position;
}

To this:

public Object getItem(int position) {
        return mThumbIds[position];
}
Sign up to request clarification or add additional context in comments.

4 Comments

still not working.i mean when dialog showed,it will preview a selected image on grid.but thanks your suggests :-)
what exactly is the problem? does it throw any exception?
i mean the image is not show in dialog, when i run and build project there is no error :-(
does it return a valid array index value when you click an item from list?
0

i think you using both different array so its possible mismatch ,

public int[] imageResto = {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4
};

  // images in array
    public Integer[] mThumbIds = {
             R.drawable.thumb1_e,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4,
             R.drawable.thumb5,
             R.drawable.thumb1,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4
    };

And put valid Image Array position is here getting is right 0 to length of Array in apter so its write position get here

 gView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            imgviewThumb.setImageResource(validImageArray[position]);
        }
    });

or You can do like this change in getView

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imageResto[position]);

        return imageView;
    }

2 Comments

i guess the issue is on imgviewPreviewDialog.setImageResource(imageResto[positions]).because there is no problem in setOnItemClick,the image has already match with selected item on grid.i mean when i click that imageview,dialog will display the image :-)
the logcat shows that there is java.lang.ArrayIndexOutOfBoundsException: length=9; index=9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.