How to pass an array of objects to an Activity?

I read posts about passing arrays from and to actions, but I'm confused about how I will do this for my specific case.

I have an array of objects called DaysWeather (an array of DaysWeather []), where the objects have several String attributes, as well as a bitmap attribute. I read somewhere that you have to make it serializable or accessible, or something like that, but at first glance it seems messy.

Can someone lead me in the right direction?

Is there an easy way to do this?

+7
android android-activity bundle
source share
1 answer

Your objects must implement the Parcelable interface .

When this is done, you can create a Parcelable array and pass it 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 implementing the Parcelable interface, do not serialize complete heavy objects. For example, for your bitmap, serialize only the ressource identifier and, when inflated, recreate the bitmap from the resource identifier.

+5
source share

All Articles