如何使用 ArrayList<Parcelable> 正确实现 Parcelable? [英] how to properly implement Parcelable with an ArrayList&lt;Parcelable&gt;?

查看:45
本文介绍了如何使用 ArrayList<Parcelable> 正确实现 Parcelable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作我的课程 Parcelable 时遇到问题.问题是,我试图将类中的成员写入包裹中,该成员是 ArrayList 对象.ArrayListSerializable,列表中的对象(ZigBeeDev)是Parcelable.

I am having trouble making my class Parcelable. The trouble is, I am trying to write to the parcel a member in the class which is an ArrayList<Parcelable> object. The ArrayList is Serializable, and the objects (ZigBeeDev) in the list are Parcelable.

相关代码如下:

package com.gnychis.coexisyst;

import java.util.ArrayList;
import java.util.Iterator;

import android.os.Parcel;
import android.os.Parcelable;

public class ZigBeeNetwork implements Parcelable {

    public String _mac;             // the source address (of the coordinator?)
    public String _pan;             // the network address
    public int _band;               // the channel
    ArrayList<Integer> _lqis;       // link quality indicators (to all devices?)
    ArrayList<ZigBeeDev> _devices;  // the devices in the network

    public void writeToParcel(Parcel out, int flags) {
        out.writeString(_mac);
        out.writeString(_pan);
        out.writeInt(_band);
        out.writeSerializable(_lqis);
        out.writeParcelable(_devices, 0);  // help here
    }

    private ZigBeeNetwork(Parcel in) {
        _mac = in.readString();
        _pan = in.readString();
        _band = in.readInt();
        _lqis = (ArrayList<Integer>) in.readSerializable();
        _devices = in.readParcelable(ZigBeeDev.class.getClassLoader());  // help here
    }

    public int describeContents() {
        return this.hashCode();
    }

    public static final Parcelable.Creator<ZigBeeNetwork> CREATOR = 
            new Parcelable.Creator<ZigBeeNetwork>() {
        public ZigBeeNetwork createFromParcel(Parcel in) {
            return new ZigBeeNetwork(in);
        }

        public ZigBeeNetwork[] newArray(int size) {
            return new ZigBeeNetwork[size];
        }
    };

    //...
}

我已经标记了两个点//help here"以了解如何正确写入包裹以及如何重建它.如果 ZigBeeDevParcelable(正确测试),我该如何正确执行此操作?

I have marked two spots "// help here" to understand how to properly write to the parcel and how to reconstruct it. If ZigBeeDev is Parcelable (properly tested), how do I do this properly?

推荐答案

你差点就搞定了!

你只需要:

public void writeToParcel(Parcel out, int flags) {
    out.writeString(_mac);
    out.writeString(_pan);
    out.writeInt(_band);
    out.writeSerializable(_lqis);
    out.writeTypedList(_devices);
}

private ZigBeeNetwork(Parcel in) {
    _mac = in.readString();
    _pan = in.readString();
    _band = in.readInt();
    _lqis = (ArrayList<Integer>) in.readSerializable();
    in.readTypedList(_devices, ZigBeeDev.CREATOR);
}

仅此而已!

对于你的整数列表,你也可以这样做:

For your list of Integer, you can also do :

out.writeList(_lqis);
_lqis = new ArrayList<>();
in.readList(_lqis Integer.class.getClassLoader());

它应该可以工作.

这篇关于如何使用 ArrayList<Parcelable> 正确实现 Parcelable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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