问题
I have read posts on passing arrays from and to activities, but I am confused as to how I would do it for my specific case.
I have an array of objects called DaysWeather (a DaysWeather[] array) where the objects have several String attributes as well as a bitmap attribute. I read somewhere that you have to make it serializable or parceable or something, but it seems messy at first glance.
Could someone lead me in the right direction?
Is there a simple way to do this?
回答1:
Your objects need to implement the Parcelable interface.
When this is done, you can create the Parcelable array and pass it to the activity:
// We assume we have an array: DaysWeather[] input;
Parcelable[] output = new Parcelable[input.length];
for (int i=input.length-1; i>=0; --i) {
output[i] = input[i];
}
Intent i = new Intent(...);
i.putExtra("myArray", output);
Also note that when you implement the Parcelable interface, do not serialize full heavy objects. For instance, for your bitmap, serialize the ressource ID only and when inflating, recreate the bitmap from the ressource ID.
来源:https://stackoverflow.com/questions/4181885/how-do-you-pass-an-object-array-to-an-activity