Android:Object.list在将其作为parcelable传递后为空

时间:2015-05-19 01:23:39

标签: java android list android-intent parcelable

我对java / android很新。我试图将一个实现Parcelable的自定义对象(Raffle)传递给另一个活动。

在接收活动中,我能够访问此对象的字符串属性,但列表似乎在某些时候被清空。在第一个活动中烘烤list.size()给出3,但接收活动中相同的toast给出0。

相关代码如下。如果我遗漏了任何东西,请告诉我。

发送活动(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);
        }
    });

接收活动(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];
  }
};



}

1 个答案:

答案 0 :(得分:2)

您似乎没有将tickerTiers写入包裹。对于自定义类型的parceling数组,这样的回答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);
}