gpt4 book ai didi

android - 传递可打包的对象数组列表

转载 作者:行者123 更新时间:2023-11-29 16:07:54 25 4
gpt4 key购买 nike

我试图通过 Parcelable 传递对象数组。但是传输之后,数据变成了奇怪的东西

传输部分:

for (int i=0; i<res.size(); i++) {
Log.d(LOG_TAG, "id = "+res.get(i).id+" weigth = "+res.get(i).weight);
}

ParcelableProducts Checked = new ParcelableProducts();
Checked.setList(res);
intent.putExtra(ParcelableProducts.class.getCanonicalName(), Checked);

接收部分:

ParcelableProducts res = (ParcelableProducts) data.getParcelableExtra(ParcelableProducts.class.getCanonicalName());
ArrayList<prProduct> prod = res.prod;
for (int i=0; i<prod.size(); i++) {
Log.d(LOG_TAG, "id = "+prod.get(i).id+" weigth = "+prod.get(i).weight);
}

类可与 ArrayList 打包:

public class ParcelableProducts implements Parcelable {
final private static String LOG_TAG = "ParcelableProducts";
public ArrayList<prProduct> prod;

public ParcelableProducts() {
prod = new ArrayList<prProduct>();
}

public void setList(ArrayList<prProduct> _prod){
prod = _prod;
}

public ArrayList<prProduct> getList() {
return prod;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeList(prod);
}
public static final Parcelable.Creator<ParcelableProducts> CREATOR = new Parcelable.Creator<ParcelableProducts>() {
public ParcelableProducts createFromParcel(Parcel in) {
return new ParcelableProducts(in);
}

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

private ParcelableProducts(Parcel parcel) {
prod = new ArrayList<prProduct>();
parcel.readTypedList(prod, prProduct.CREATOR);
}
}

和 prProduct:

public class prProduct implements Parcelable {
final static String LOG_TAG = "prProduct";

public float weight;
public int id;

public prProduct(int _id, Float _weight) {
weight = _weight;
id = _id;
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel parcel, int flags) {
parcel.writeFloat(weight);
parcel.writeInt(id);
}

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

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

private prProduct(Parcel parcel) {
weight = parcel.readFloat();
id = parcel.readInt();
}
}

在日志中:

传输: id = 7 weigth = 0.0

接收: id = 7602278 weigth = 4.2E-44

最佳答案

目前我没有看到数据在传输过程中被破坏的地方,但这将有助于清理您的代码。就个人而言,我在过去重构代码以提高可读性时修复了许多奇怪的错误。您应该做的第一件事是删除类“ParcelableProducts”,因为您不需要它。只需使用 putParcelableArrayListExtra 方法直接在 Intent 中传递 ArrayList。 Shown here.

此外,这有点挑剔,但您不应该直接访问您的字段。最好将它们设置为私有(private)并使用 getters/setters。此外,为您的日志语句使用 for each 循环会更简洁一些。

更安全的 PrProduct 类。

//Java classes should start with capital letter
public class PrProduct implements Parcelable {
private final static String LOG_TAG = "PrProduct";

private float weight;
private int id;

public prProduct(int id, float weight) {
this.weight = weight;
this.id = id;
}
//Using getters only makes this immutable which is safer since the
//weight/id aren't likely to change.
public float getWeight(){
return weight;}
public int getId(){
return id;}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel parcel, int flags) {
parcel.writeFloat(weight);
parcel.writeInt(id);
}

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

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

private prProduct(Parcel parcel) {
weight = parcel.readFloat();
id = parcel.readInt();
}
}

//A sample for each loop
for(PrProduct product: prod)
Log.d(LOG_TAG, "weight=" + product.getWeight() + " id=" + product.getId());

关于android - 传递可打包的对象数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16432340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com