Program Tip

ArrayList로 Parcelable을 올바르게 구현하는 방법

programtip 2020. 10. 18. 19:03
반응형

ArrayList로 Parcelable을 올바르게 구현하는 방법?


수업을 듣는 데 문제가 Parcelable있습니다. 문제는 ArrayList<Parcelable>객체 인 클래스의 구성원을 소포에 쓰려고한다는 것 입니다. ArrayList것입니다 Serializable, 그리고 객체 ( ZigBeeDev목록)이 있습니다 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"두 곳을 표시했습니다. 경우 ZigBeeDevIS가 Parcelable(제대로 테스트), 어떻게 적절하게이 작업을 수행합니까?


거의 얻었습니다!

다음을 수행하면됩니다.

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

그게 다야!

정수 목록의 경우 다음을 수행 할 수도 있습니다.

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

작동합니다.


내 경우에는 in.readTypedList(_devices, ZigBeeDev.CREATOR);나에게 준 NullPointerException에를 _devices. 그래서 이것을 사용했습니다.

_devices = in.createTypedArrayList(ZigBeeDev.CREATOR);

You should use writeList(List l) for your list of integers and writeTypedList(List val) for the list of ZigBeeDevices


In constructor you should use

_lqis = in.createTypedArrayList(ZigBeeDev.CREATOR);

And in "writeToParcel" use

out.writeTypedList(_lqis);

A little late but I also had this issue. After a long waste of time I stumbled upon the parcelabler.com website which automatically creates parcels for you.

It allowed me to create a nested parcel with an array list inside very easily, and saved me a lot of time.

Basically the way the website works is you enter your object with the ArrayList in it and it automatically adds the necessary methods to make it parcelable (read from parcel, write to parcel, describe contents, and parcel Creator are all generated automatically). This is particularily useful when creating complex parcels, such as the question here asks, which contain nested parcels, arrays, and lists.

EDIT: Also IntelliJ IDEA and Android Studio have plugins for this which do a similar thing to the website listed:

These plugins generate Android Parcelable boilerplate code based on fields in the class.

참고URL : https://stackoverflow.com/questions/7042272/how-to-properly-implement-parcelable-with-an-arraylistparcelable

반응형