你如何传递一个对象数组的活动? [英] How do you pass an object array to an Activity?

查看:93
本文介绍了你如何传递一个对象数组的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从和活动传递数组读职位,但我很困惑,我会怎么做它为我的特殊情况。

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.

予有称为DaysWeather(一个DaysWeather []数组)对象,其中所述对象具有几个串属性以及位图属性的阵列。我读的地方,你必须让它序列化或parceable或东西,但它乍看起来非常凌乱。

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?

推荐答案

您的对象需要实现的 Parcelable接口。

当做到这一点,你可以创建Parcelable数组并把它传递给活动:

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);

另外请注意,当你实现Parcelable接口,不完整的序列化重物。例如,对于您的位图,只有序列化的ressource ID和充气的时候,重新从的ressource ID位图。

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.

这篇关于你如何传递一个对象数组的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆