如何解决对象的数组传递给其他Acitvity [英] How to pass an array of Address objects to an other Acitvity

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

问题描述

喜 我想通过一个Intent对象传递的地址对象到另一个活动的阵列。

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

由于地址类实现 Parcelable 接口,我尽量做到以下几点。我从地理codeR对象,我转换为地址对象的数组列表地址对象。然后我把这个数组的意图和调用活动。

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

在其他活动我尝试检索地址[]从意图与下面这段code。但是,最后一行的电话与一个ClassCastException Landroid.os.Parcelable 结束。

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[].

推荐答案

现在的问题是铸造。尝试:

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

这篇关于如何解决对象的数组传递给其他Acitvity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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