Mohsin Mushtaq
Mohsin Mushtaq

Reputation: 133

Getting gridview image in new activity

Im trying to send my clicked/selected image to new activity here's my code

public int getCount() {
    return mThumbIds.length;
}
public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView; 
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(mContext,HeroData.class);
                    intent.putExtra("imageID", position);
                    mContext.startActivity(intent);
                }
            });
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;

    }

private Integer[] mThumbIds = {
        R.drawable.pic1, R.drawable.pic12,
        R.drawable.pic2, R.drawable.pic13,
        R.drawable.pic3, R.drawable.pic14,

};

}

and trying to receive it in new activity

Bundle bdl = getIntent().getExtras();
    int index = bdl.getInt("imageID");
    ImageView image = (ImageView) findViewById(R.id.imageview);
    image.setImageResource();//the problem ! how to receive the selected image

i want to know im sending ("imageID",position); how to get the selected image

Upvotes: 0

Views: 2565

Answers (3)

cooperok
cooperok

Reputation: 4257

You can keep your drawable resourses in another place, for example you can do it in resources, as string-array

<resources>

<string-array name="thumbs_imgs">
    <item>@drawable/pic1</item>
    <item>@drawable/pic2</item>
    <item>@drawable/pic3</item>
</string-array>

</resources>

and then get by index drawable what you need

TypedArray imgs = getResources().obtainTypedArray(R.array.thumbs_imgs);
//get resourceid by index
mImgView1.setImageResource(imgs.getResourceId(i, -1));

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Use Like this...

gridView.setOnItemClickListener(new OnItemClickListener()   
    {

        public void onItemClick(AdapterView<?> parent, View v,int position, long id) 

        {

            Intent i = new Intent(getApplicationContext(), HeroData.class);



            i.putExtra("imageID", position);

             startActivity(i);


        }
    });

need to wright click for gridview...

Upvotes: 1

Since you are sending resources, you can send the resource id instead of the index it has in the array. Something like this:

intent.putExtra("imageID", mThumbIds[position]);
mContext.startActivity(intent);

And in your activity:

Bundle bdl = getIntent().getExtras();
int imageRes = bdl.getInt("imageID");
ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageResource(imageRes);

Hope that helps.

Upvotes: 2

Related Questions