Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing arrays of Parcelables to a Parcel in Android

I'm trying to write an array of objects that implement Parcelable into a Parcel using writeParcelableArray.

The objects I'm trying to write are defined (as you'd expect) as:

public class Arrival implements Parcelable {
    /* All the right stuff in here... this class compiles and acts fine. */
}

And I'm trying to write them into a `Parcel' with:

@Override
public void writeToParcel(Parcel dest, int flags) {
    Arrival[] a;
    /* some stuff to populate "a" */
    dest.writeParcelableArray(a, 0);
}

When Eclipse tries to compile this I get the error:

Bound mismatch: The generic method writeParcelableArray(T[], int) of type Parcel is not applicable for the arguments (Arrival[], int). The inferred type Arrival is not a valid substitute for the bounded parameter < T extends Parcelable >

I completely don't understand this error message. Parcelable is an interface (not a class) so you can't extend it. Anyone have any ideas?

UPDATE: I'm having basically the same problem when putting an ArrayList of Parcelables into an Intent:

Intent i = new Intent();
i.putParcelableArrayListExtra("locations", (ArrayList<Location>) locations);

yields:

The method putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) in the type Intent is not applicable for the arguments (String, ArrayList< Location >)

This may be because Location was the class I was working on above (that wraps the Arrivals), but I don't think so.

like image 826
Jeremy Logan Avatar asked Dec 11 '09 21:12

Jeremy Logan


1 Answers

Actually, you can extend an interface, and it looks like you need to do just that. The generics parameter in writeParcelableArray is asking for an extended interface (not the interface itself). Try creating an interface MyParcelable extends Parcelable. Then declaring your array using the interface, but the impl should be your Arrival extends MyParcelable.

like image 151
harschware Avatar answered Oct 04 '22 22:10

harschware