5

I'm trying to pass an array of Address objects to another Activity through an Intent object.

As the Address class implements the Parcelable interface I try to do the following. I got a List Address object from a Geocoder object, which I convert into a array of Address objects. Then I put this array into the Intent and call the activity.

final Address[] addresses = addresseList.toArray(new Address[addresseList.size()]);

final Intent intent = new Intent(this, SelectAddress.class);
intent.putExtra(SelectAddress.INTENT_EXTRA_ADDRESSES, startAddresses);

startActivityForResult(intent, REQUEST_CODE_ACTIVITY_SELECT_ADDRESSES);

On the other activity I try to retrieve the Address[] from the Intent with the following piece of code. But the call of the last line ends with a ClassCastException Landroid.os.Parcelable.

Bundle bundle = getIntent().getExtras();            
Address[] addresses = (Address[]) bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);

What am I doing wrong? How do I have to retrieve the Address[].

Flo
  • 26,717
  • 14
  • 82
  • 124
  • "Hi I'm trying to pass an array of Address objects to another Activity through an Intent object." -- why? To me, passing complex objects between activities is a code smell. Think of `Intent` extras as being GET parameters on a URL. If these were two Web pages, would you be passing a bunch of `Address` objects as GET parameters on a URL in a link? Assuming that passing an array of `Address` objects is unavoidable, what is the type of object you're getting back? The `ClassCastException` line should tell you that. – CommonsWare Sep 05 '10 at 19:34
  • The type mentioned by the ClassCastException is a Landroid.os.Parcelable. – Flo Sep 05 '10 at 19:50
  • Regarding you concerns about passing an array of complex objects to an activity. What I want to do is providing the user an second activity where he can select from a list of addresses, when the search for an address in the first activity has returned multiple results. How would you implement this without passing the addresses to the second activity to display them in a spinner, so the user can select one of them? – Flo Sep 05 '10 at 20:02
  • @CommensWare: So switching the layouts and views in a single activity would do the trick? Why no second activity? – Flo Sep 06 '10 at 07:52
  • This solved my problem of passing object array [1]: http://stackoverflow.com/questions/8745893/i-dont-get-why-this-classcastexception-occurs/8745966#8745966 – Atul Bhardwaj Dec 28 '12 at 07:15

3 Answers3

14

The problem is the casting. try:

Bundle bundle = getIntent().getExtras();
Parcelable[] parcels = bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);

Address[] addresses = new Address[parcels.length];
for (Parcelable par : parcels){
     addresses.add((Address) par);              
}
Ken Y-N
  • 12,690
  • 21
  • 62
  • 98
LiorZ
  • 331
  • 1
  • 5
  • 15
  • 9
    The answer is correct, but since when did you could use ".add" to add to an array? lol use this: ** `for(int i=0;i – Shehaaz Mar 29 '13 at 02:51
5
or on java1.6:
    Parcelable[] x = bundle.getParcelableArray(KEY);
    addresses = Arrays.copyOf(x, x.length, Address[].class);
user3746807
  • 51
  • 1
  • 1
  • 3
    Welcome to StackOverflow! Even though you have provided an answer, it is best to clarify a bit more on what your solution provides, and how it helps the OP with their question. – Mike Koch Jun 17 '14 at 02:02
  • It helps in the way you don't need to write a loop. One line instead. – Malachiasz Feb 25 '15 at 09:01
1

The @LiorZ answer is completely true. I just merged his answer and this other in this handy function.

@SuppressWarnings("unchecked")
private static <T extends Parcelable> T[] castParcelableArray(Class<T> clazz, Parcelable[] parcelableArray) {
    final int length = parcelableArray.length;
    final T[] array = (T[]) Array.newInstance(clazz, length);
    for (int i = 0; i < length; i++) {
        array[i] = (T) parcelableArray[i];
    }
    return array;
}
Community
  • 1
  • 1
Brais Gabin
  • 5,447
  • 5
  • 50
  • 88