개체 목록이 포함 된 구획 가능 개체를 전달하는 방법은 무엇입니까?
Parcelable
아래 에 개체를 만들었습니다. 개체에 List
의 제품이 포함되어 있습니다 . 내 생성자에서 어떻게 처리하나요 다시 만드는 내를 Parcelable
위해 List
?
소포에서 사용할 수있는 모든 방법을 확인했으며 사용할 수있는 모든 방법은 readArrayList(ClassLoader)
입니다. 이것이 최선의 접근 방식인지 잘 모르겠습니다. 귀하의 조언은 정말 감사하겠습니다.
public class Outfits implements Parcelable {
private String url;
private String name;
private List<Product> products;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public void writeToParcel(Parcel dest, int flags) {
Log.v("", "writeToParcel..." + flags);
dest.writeString(url);
dest.writeString(name);
dest.writeList(products);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Outfits createFromParcel(Parcel in) {
return new Outfits(in);
}
public Outfits[] newArray(int size) {
return new Outfits[size];
}
};
@Override
public int describeContents() {
return 0;
}
/*** Here how do I populate my List of Products ***/
private Outfits(Parcel in) {
url = in.readString();
name = in.readString();
products = in.read ???????;
}
}
클래스 Product
가 Parcelable 프로토콜과 호환되는 경우 문서에 따라 다음과 같이 작동해야합니다.
products = new ArrayList<Product>();
in.readList(products, Product.class.getClassLoader());
먼저 Product 객체는 Parcelable
.
그리고, 사용 dest.writeTypedList(products)
의 writeToParcel()
방법.
마지막으로 다음 코드를 사용하여 목록을 구문 분석합니다.
products = new ArrayList<Product>();
in.readTypedList(products, Product.CREATOR);
자세한 내용은 공식 문서 를 참조하십시오 .
이것은 작동합니다.
in.readTypedList(products, Product.CREATOR);
In my personal experience, http://www.parcelabler.com/ is an amazing site for this. You just create your class, and copy paste it into the website, and it generates a Parcelable version of your class.
I tested it with a class named "Theme" that contained the following variables:
private String name;
private int image;
private List<Card> cards;
The writeToParcel function becomes:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(image);
if (cards == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(cards);
}
}
generated constructor:
protected Theme(Parcel in) {
name = in.readString();
image = in.readInt();
if (in.readByte() == 0x01) {
cards = new ArrayList<Card>();
in.readList(cards, Card.class.getClassLoader());
} else {
cards = null;
}
}
EDIT: make sure that the Card object is also Parcelable!
implement Parcelable
on the Product class as well and then
in.readList(this.list, Product.class.getClassLoader());
if any of the above solutions didn't work.
The other way is to use readValue&writeValue.
protected Product (Parcel in) {
...
in.readValue(this.getClass().getClassLoader());
}
@Override
public void writeToParcel(Parcel parcel, int i) {
...
parcel.writeValue(**list**);
}
Items of the list should implement Parcelable
Here you go...
Make sure "Products.java" should be extended with Parcelable
Step 1:
private List<Products> products;
Step 2:
private Outfits(Parcel in) {
products = in.createTypedArrayList(Products.CREATOR);
}
Step 3:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(products);
}
Assuming the Product
is implementing Parcelable
, you can use this for writing:
dest.writeValue(products);
and this for reading:
products = (List<Product>) in.readValue(Product.class.getClassLoader());
Product must be implements Parcelable
Product class implements Parcelable {
......
}
Then write you object contains list like
public class Outfits implements Parcelable {
private String url;
private String name;
private List<Product> products;
public Outfits (Parcel pi) {
bookName = p.readString();
bookId = p.readInt();
isColor = p.readInt() == 1;
//use this well be get err
//products=p.readArrayList(Thread.currentThread().getContextClassLoader());
//Pass list use this
products= in.createTypedArrayList(Product.CREATOR);
}
...get and set...
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(url);
dest.writeString(name);
//use this will no working
//dest.writeList(products);
//Parcelable list
out.writeTypedList(products);
}
}
'program story' 카테고리의 다른 글
node-jwt-simple이있는 passport-local (0) | 2020.09.12 |
---|---|
ELF 파일과 bin 파일의 차이점은 무엇입니까? (0) | 2020.09.12 |
CSS로 div에 이미지를 넣는 방법은 무엇입니까? (0) | 2020.09.12 |
Hibernate : session.get과 session.load의 차이점 (0) | 2020.09.12 |
PowerShell에서 파일을 한 줄씩 스트림으로 처리하는 방법 (0) | 2020.09.12 |