0

I want to put an int[] into an Android Intent. For various reasons I am getting a LinkedList<Integer>, so I tried (roughly) this:

LinkedList<Integer> myList = getIntegerList();
Intent intent = new Intent (context, class);
intent.putExtra("com.me.INTARRAY",  myList.toArray());

This didn't work because toArray() was giving me an Object[]. I thought I could fix it by doing

intent.putExtra("com.me.INTARRAY", (Integer[]) myList.toArray(new Integer[myList.size()]) )

which does indeed produce an Integer[]; however, getting it out of the Intent STILL doesn't work.

Ultimately this isn't too hard to work around with a for loop so I ask more for curiosity than any other reason, but ... how do you get an Integer[] into an Intent extra as though it were an int[]?

Andrew Wyld
  • 7,133
  • 7
  • 54
  • 96

2 Answers2

3

I have not tested this thoroughly, but I believe it should work. You can try it.

Integer[] integerArray = {/*something */ };
intent.putExtra("com.me.INTARRAY",  integerArray);

And then (not working)

Integer[] newArray = (Integer[]) getIntent().getSerializableExtra("com.me.INTARRAY");

EDIT: Ahh.. After a little research and testing it seems the above is not possible. It is not possible because the compiler would have to iterate through every element to verify the cast. A solution(and I have testet it this time) would be to do this instead:

Object[] s = (Object[]) getIntent().getSerializableExtra("com.me.INTARRAY");
Integer[] newArray = Arrays.copyOf(s, s.length, Integer[].class);

I found the explanation here: Deserializing Arrays on Android

Community
  • 1
  • 1
MAV
  • 7,260
  • 4
  • 30
  • 47
0

You can change the array to int[] and pass it as extras using putExtra(java.lang.String, int[]). On the other side you will need to get that extra using getIntArrayExtra.

Here is the code that works fine, caller first and receiver follows:

LinkedList<Integer> myList = getIntegerList();;
int iArray[] = new int[myList.size()];
for (Integer i : myList)
iArray[myList.indexOf(i)] = i;
Intent intent = new Intent();// create your actual intent
intent.putExtra("com.your.package.name.int", iArray);
startActivity(intent);

and the other side:

int array[] = getIntent().getExtras().getIntArray("com.your.package.name.int");

Another alternate, (which is better i guess) is to convert the LinkedList to Integer ArrayList. This works as follows, sender first receiver followed.

intent.putIntegerArrayListExtra("com.your.package.name.array", new ArrayList<Integer>(myList));

and on the other side:

ArrayList<Integer> array = getIntent().getExtras().getIntegerArrayList("com.your.package.name.array");

Hope this helps :)

Hassan
  • 1,002
  • 15
  • 21
  • Not really I'm afraid, as this is exactly what I tried, and failed, to do. In the end I used a `for` loop to insert the elements of the `LinkedList` into an array. – Andrew Wyld Jun 12 '12 at 12:04
  • @Andrew Wyld: I'm afraid you would have used it wrong. Its for int[] and not Integer[]. I tried it at my end and works fine. Will also edit my post to add example code for you. – Hassan Jun 13 '12 at 13:46
  • @AndrewWyld: The second alternative will rid you of the loop. And if this helps and works for you, remember to accept the answer :) – Hassan Jun 13 '12 at 13:57
  • That's exactly what I did. I wanted to know if you could get the array WITHOUT doing this. – Andrew Wyld Jun 13 '12 at 20:19
  • What I failed to do was cast the array. I was hoping it would be unnecessary to create a completely new array. – Andrew Wyld Jun 13 '12 at 20:20