我正在制作一个非盈利性慈善应用程序。尽管我检查了堆栈和Google中的许多问题,但我无法解决问题。
我有3节课:
 -BaseCell实现Parcelable(基类)
 -Needy扩展BaseCell
 -包含所有类列表的UserBasket类扩展了BaseCell

问题

我在UserBasket类中使用Arraylist持有Needy类。当我将其发送到另一个活动时,如果我在UserBasket中添加1个项目,我会得到荒谬的结果(缺少或错误的字符),如果我添加了1个以上的项目,那么我会得到异常。

我需要将UserBasket类(有需要的物品清单)交付给付款活动,以便我可以计算出慈善总价并采取必要的行动。

public abstract class BaseCell implements Parcelable {


String imageUrl;
int percentageOfCollectedDonation;
String needyTitle;
String needyDescription;

protected String category;
protected int amountOfCollectedDonation=0;
protected int amountOfTargetDonation=0;
protected int amountOfDonater=0;
protected int drawableID;
protected String campaignCode;

protected int maxInstallmentNumber;

int price;

public String getCellType() {
    return cellType;
}

protected String cellType ;

/**
 * How many of this campaign purchased by user
 * */
protected int userPurchaseAmount = 1;

protected BaseCell(String cellType)
{
    this.cellType = cellType;
}
protected BaseCell(Parcel in)
{

    drawableID = in.readInt();
    price = in.readInt();
    imageUrl = in.readString();
    needyTitle = in.readString();
    needyDescription = in.readString();
    category = in.readString();
    campaignCode = in.readString();
    maxInstallmentNumber = in.readInt();
    userPurchaseAmount = in.readInt();
}

public static final Parcelable.Creator<BaseCell> CREATOR = new Parcelable.Creator<BaseCell>() {
    @Override
    public BaseCell createFromParcel(Parcel in) {
        String cellType = in.readString();
        BaseCell baseCell = null;

        if (cellType.equals("Needy"))
        {
            baseCell = (Needy)new Needy(in);
        }else
        if (cellType.equals("Qurban"))
        {
            baseCell = (Qurban)new Qurban(in);
        }

        return baseCell;
    }

    @Override
    public BaseCell[] newArray(int size) {
        return new BaseCell[size];
    }
};
public void writeToParcel(Parcel out, int flags) {
    out.writeString(getCellType());
}


public BaseCell(String imageUrl, int drawableID, String needyTitle, String needyDescription, int amountOfCollectedDonation, int amountOfTargetDonation, int amountOfDonater, String category,String campaignCode, int maxInstallmentNumber, int price)
{
    this.imageUrl = imageUrl;
    this.drawableID = drawableID;
    this.needyTitle = needyTitle;
    this.needyDescription = needyDescription;
    this.amountOfCollectedDonation = amountOfCollectedDonation;
    this.amountOfTargetDonation = amountOfTargetDonation;
    this.amountOfDonater = amountOfDonater;
    this.category = category;
    this.campaignCode = campaignCode;
    this.maxInstallmentNumber = maxInstallmentNumber;
    this.price= price;
}


}




有需要的

    public class Needy extends BaseCell {

    protected Needy(Parcel in) {
    super(in);
    cellType ="Needy";
    }



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

    @Override
    public Needy[] newArray(int size) {
        return new Needy[size];
    }
};


public Needy(String imageUrl, int drawableID, String needyTitle, String needyDescription, int amountOfCollectedDonation, int amountOfTargetDonation, int amountOfDonater, String category, String campaignCode, int maxInstallmentNumber, int price) {

    super(imageUrl, drawableID, needyTitle, needyDescription, amountOfCollectedDonation, amountOfTargetDonation, amountOfDonater, category, campaignCode, maxInstallmentNumber,price);
    cellType = "Needy";
}


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(getCellType());
    super.writeToParcel(dest,flags);
    dest.writeInt(drawableID);
    dest.writeInt(price);
    dest.writeString(imageUrl);
    dest.writeString(needyTitle);
    dest.writeString(needyDescription);
    dest.writeString(category);
    dest.writeString(campaignCode);
    dest.writeInt(maxInstallmentNumber);
    dest.writeInt(userPurchaseAmount);
}

@Override
public void setUserPurchaseAmount(int userPurchaseAmount) {
    super.setUserPurchaseAmount(userPurchaseAmount);
 }
}




用户篮

    public class UserBasket implements Parcelable{


List<BaseCell> userBasket;

/**
 * holds all items to be purchased
 * */
public UserBasket(List<BaseCell> userBasket) {
    this.userBasket = userBasket;
}

public UserBasket() {
    userBasket = new ArrayList<>();
}


protected UserBasket(Parcel in) {
    super();
    setUserBasket(new ArrayList<BaseCell>());
    userBasket = in.createTypedArrayList(BaseCell.CREATOR);
    //in.readTypedList(userBasket,BaseCell.CREATOR);
}

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

    @Override
    public UserBasket[] newArray(int size) {
        return new UserBasket[size];
    }
};

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedList(userBasket);
}



public List<BaseCell> getUserBasket() {
    return userBasket;
}

public void setUserBasket(List<BaseCell> userBasket) {
    this.userBasket = userBasket;
}

/**
 * Add to the basket list
 * */
public void add(Needy donation) {
    if (donation != null)
        userBasket.add(donation);
}

/**
 * Remove from basket list
 * */
public void remove(int position)
{
    if (userBasket.size()>0)
        userBasket.remove(position);
}

}



从MainActivity发送userBasket arrayList与项目
        Navigate.navigateToPaymentPayuStart
        (MainActivity.this,userBasket,“购物篮”);
        //使用“ basket”键的userBasket arrayList
        在payPayu活动中接收UserBasket
        UserBasket userBasket = getIntent()。getParcelableExtra(“ basket”);
我如何在PaymentPayuActivity中正确获取UserBasket。
感谢您的帮助
谢谢。

最佳答案

看来您在处理Parcelable实现错误。 e.i.您将两次从Parcel中读取某些变量,这是不允许的。因此,为了改善这一点,我们将尝试使您的代码更简单。由于Needy实际上没有任何变量,因此让BaseCell处理所有可打包实现似乎更好,我尝试为您创建一个模拟程序,因此根据您的其余代码,可能需要一个一点调整。

首先,我删除了Parcelable中的所有Needy实现,只是将其CREATOR指向BaseCell

public class Needy extends BaseCell {

    protected Needy(Parcel in) {
        super(in);
        cellType ="Needy";
    }

    // Since Needy doesn't actually store any variables, we don't need a Creator for it.
    // Just point it to BaseCell.CREATOR and let it handle it
    public static final Parcelable.Creator<BaseCell> CREATOR = BaseCell.CREATOR;

    public Needy(String imageUrl, int drawableID, String needyTitle, String needyDescription, int amountOfCollectedDonation, int amountOfTargetDonation, int amountOfDonater, String category, String campaignCode, int maxInstallmentNumber, int price) {
        super(imageUrl, drawableID, needyTitle, needyDescription, amountOfCollectedDonation, amountOfTargetDonation, amountOfDonater, category, campaignCode, maxInstallmentNumber,price);
        cellType = "Needy";
    }

}


然后,让BaseCell处理所有Parcelable实现,如下所示:

public abstract class BaseCell implements Parcelable {

    /**
     * ALL OF YOUR VARIABLES, GETTERS, SETTERS AND CONSTRUCTORS GOES HERE
     */

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

    public static final Parcelable.Creator<BaseCell> CREATOR = new Parcelable.Creator<BaseCell>() {
        @Override
        public BaseCell createFromParcel(Parcel in) {
            String cellType = in.readString();
            if (cellType.equals("Needy")) {
                return (Needy)new Needy(in);
            } else if (cellType.equals("Qurban")) {
                return (Qurban)new Qurban(in);
            }
            return null;
        }

        @Override
        public BaseCell[] newArray(int size) {
            return new BaseCell[size];
        }
    };

    protected BaseCell(Parcel in) {
        drawableID = in.readInt();
        price = in.readInt();
        imageUrl = in.readString();
        needyTitle = in.readString();
        needyDescription = in.readString();
        category = in.readString();
        campaignCode = in.readString();
        maxInstallmentNumber = in.readInt();
        userPurchaseAmount = in.readInt();
    }

    public void writeToParcel(Parcel out, int flags) {
        // cellType written first, and read by Creator
        out.writeString(cellType);
        // the rest is read by the BaseCell constructor
        out.writeInt(drawableID);
        out.writeInt(price);
        out.writeString(imageUrl);
        out.writeString(needyTitle);
        out.writeString(needyDescription);
        out.writeString(category);
        out.writeString(campaignCode);
        out.writeInt(maxInstallmentNumber);
        out.writeInt(userPurchaseAmount);
    }

}

10-04 23:09