0

I just started to learn java. I am trying to transfer an int array of drawable images from a class to another. this is what I tried:

The first class as follows

Intent intent = new Intent(context, target.class);
intent.putExtra("symbols", symbols);
            context.startActivity(intent);

The target class as follows

int symbols = getIntent().getIntExtra("symbols", 0);

    ImageView iv = (ImageView) findViewById(R.id.himg);
    iv.setImageResource(symbols);

when I ran it, the image didn't show up in the target class. Any solution?

nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • 1
    Possible duplicate of [Passing an array with android-intent](http://stackoverflow.com/questions/27215000/passing-an-array-with-android-intent) – Andreas Brunnet Aug 10 '16 at 04:14

1 Answers1

1

Try this You can send & read Integer array using intent.

Source Activity

int myDrawableArray[] = {R.drawable.icon_home,R.drawable.icon_search,R.drawable.icon_plus};
Intent switchIntent = new Intent(A.this, B.class);
switchIntent.putExtra("myDrawableArray", myDrawableArray);
startActivity(switchIntent);

Destination Activity

Bundle extras = getIntent().getExtras();
int[] myDrawableArray = extras.getIntArray("myDrawableArray");

Use image from array :

ImageView iv = (ImageView) findViewById(R.id.himg);
iv.setImageResource(myDrawableArray[2]);

I have use static index of array you can pass index via intent as per requirement.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85