如何解决对象的数组传递给其他Acitvity数组、如何解决、对象、Acitvity

2023-09-12 22:23:12 作者:国产大宝贝

喜 我想通过一个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);              
}