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"두 곳을 표시했습니다. 경우 ZigBeeDev
IS가 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:
- ★ Android Parcelable code generator (Apache License 2.0)
- Auto Parcel (The MIT License)
- SerializableParcelable Generator (The MIT License)
- Parcelable Code Generator (for Kotlin) (Apache License 2.0)
These plugins generate Android Parcelable boilerplate code based on fields in the class.
'Program Tip' 카테고리의 다른 글
의존성 주입 및 싱글 톤 디자인 패턴 (0) | 2020.10.18 |
---|---|
선언 된 패키지가 예상 패키지 ""와 일치하지 않습니다. (0) | 2020.10.18 |
Java에서 Iterable 크기 가져 오기 (0) | 2020.10.18 |
명령 줄에서 SOAP wsdl 웹 서비스 호출을 수행하는 방법 (0) | 2020.10.18 |
Android Studio의 Gson 라이브러리 (0) | 2020.10.18 |