gpt4 book ai didi

java - Android:Object.list 在作为 parcelable 传递后为空

转载 作者:行者123 更新时间:2023-11-29 20:46:14 24 4
gpt4 key购买 nike

我是 java/android 的新手。我正在尝试将实现 Parcelable 的自定义对象 (Raffle) 传递给另一个 Activity 。

在接收 Activity 中,我能够访问该对象的字符串属性,但列表在某些时候似乎是空的。在第一个 Activity 中 toast list.size() 给出 3,但在接收 Activity 中同样的 toast 给出 0。

相关代码如下。如果我遗漏了什么,请告诉我。

发送 Activity (HomeActivity)中的onCLickListener

    raffleListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(HomeActivity.this,RafflePurchaseActivity.class);
Raffle selectedRaffle = (Raffle) allRaffles.get(position);
Toast.makeText(getApplicationContext(), "1: " + selectedRaffle.Description + ": " + Integer.toString(selectedRaffle.getTicketTiers().size()), Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), "TEST: " + Integer.toString(selectedRaffle.ticketTiers.size()), Toast.LENGTH_SHORT).show();

intent.putExtra("raffle", selectedRaffle);

startActivity(intent);
}
});

接收 Activity (RafflePurchaseActivity)

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_raffle_purchase);

Bundle bundle = this.getIntent().getExtras();
Raffle theRaffle = bundle.getParcelable("raffle");

List<RaffleTicketTier> xticketTiers = theRaffle.getTicketTiers();
Toast.makeText(getApplicationContext(), "2:" + theRaffle.Description + ": " + Integer.toString(xticketTiers.size()), Toast.LENGTH_SHORT).show();
}

抽奖类

public class Raffle implements Parcelable {
public String Title;
public String Description;
public int TicketLimit;
public int numPrizes;
public String prize1;
public String prize2;
public String prize3;
public String prize4;
private List<RaffleTicketTier> ticketTiers = new ArrayList<RaffleTicketTier>();


public Raffle() {
Title = "Art Union";
Description = "THIS IS A RAFFLE DESCRIPTION";

ticketTiers.add(new RaffleTicketTier(1,10.00));
ticketTiers.add(new RaffleTicketTier(3,20.00));
ticketTiers.add(new RaffleTicketTier(8,50.00));
ticketTiers.add(new RaffleTicketTier(15,100.00));

testList.add("Test1");
testList.add("Test2");
testList.add("Test3");
}

//================================================================================
// Getters/Setters
//================================================================================

public List<RaffleTicketTier> getTicketTiers() {
return ticketTiers;
}

//================================================================================
// Parcelable methods
//================================================================================

//Constructor to use when re-constructing object from a parcel
public Raffle(Parcel in) {
readFromParcel(in);
}


@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Title);
dest.writeString(Description);
dest.writeInt(TicketLimit);
dest.writeInt(numPrizes);
dest.writeString(prize1);
dest.writeString(prize2);
dest.writeString(prize3);
dest.writeString(prize4);

}

private void readFromParcel(Parcel in) {

Title= in.readString();
Description= in.readString();
TicketLimit= in.readInt();
numPrizes= in.readInt();
prize1= in.readString();
prize2= in.readString();
prize3= in.readString();
prize4= in.readString();

}

public static final Parcelable.Creator<Raffle> CREATOR = new Parcelable.Creator<Raffle>() {

public Raffle createFromParcel(Parcel in) {
return new Raffle(in);
}

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



}

最佳答案

您似乎没有向包裹写入 tickerTiers。类似这个答案 https://stackoverflow.com/a/10781119/156708用于分割自定义类型的数组。

其他答案的相关代码

public void writeToParcel(Parcel out, int arg1) {
out.writeInt(mObjList.length);
out.writeTypedArray(mObjList, arg1);
}

private void readFromParcel(Parcel in) {
int size = in.readInt();
mObjList = in.readTypedArray(new MyClass[size], MyClass.CREATOR);
}

关于java - Android:Object.list 在作为 parcelable 传递后为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315281/

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