如何将 Address 对象数组传递给其他 Acitvity

发布于 2024-09-17 11:40:10 字数 974 浏览 4 评论 0原文

我试图通过 Address 对象数组传递给另一个 Activity一个意图对象。

由于 Address 类实现 Parcelable 接口,我尝试执行以下操作。我从地理编码器对象中获取了列表地址对象,并将其转换为地址对象数组。然后我将这个数组放入 Intent 中并调用该活动。

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

在另一个活动中,我尝试使用以下代码从 Intent 中检索 Address[]。但最后一行的调用以 ClassCastException Landroid.os.Parcelable 结束。

Bundle bundle = getIntent().getExtras();            
Address[] addresses = (Address[]) bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);

我做错了什么?我如何检索地址[]。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝咒 2024-09-24 11:40:10

问题在于铸造。尝试:

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

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);              
}
傾旎 2024-09-24 11:40:10
or on java1.6:
    Parcelable[] x = bundle.getParcelableArray(KEY);
    addresses = Arrays.copyOf(x, x.length, Address[].class);
or on java1.6:
    Parcelable[] x = bundle.getParcelableArray(KEY);
    addresses = Arrays.copyOf(x, x.length, Address[].class);
_失温 2024-09-24 11:40:10

@LiorZ 答案 完全正确。我刚刚将他的答案和这个其他合并到这个方便的函数中。

@SuppressWarnings("unchecked")
private static <T extends Parcelable> T[] castParcelableArray(Class<T> clazz, Parcelable[] parcelableArray) {
    final int length = parcelableArray.length;
    final T[] array = (T[]) Array.newInstance(clazz, length);
    for (int i = 0; i < length; i++) {
        array[i] = (T) parcelableArray[i];
    }
    return array;
}

The @LiorZ answer is completely true. I just merged his answer and this other in this handy function.

@SuppressWarnings("unchecked")
private static <T extends Parcelable> T[] castParcelableArray(Class<T> clazz, Parcelable[] parcelableArray) {
    final int length = parcelableArray.length;
    final T[] array = (T[]) Array.newInstance(clazz, length);
    for (int i = 0; i < length; i++) {
        array[i] = (T) parcelableArray[i];
    }
    return array;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文