简体   繁体   中英

Android: Object.list is empty after passing it as parcelable

I'm fairly new to java/android. I am attempting to pass a custom object (Raffle), which is implementing Parcelable, to another activity.

In the receiving activity I am able to access string properties of this object, but lists appear to be emptied at some point. Toasting the the list.size() in the first activity gives 3, but the same toast in the receiving activity gives 0.

Relevant code is below. Please let me know if I have left out anything.

onCLickListener in Sending Activity (HomeActivity)

    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);
        }
    });

Receiving 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();
}

the Raffle class

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];
  }
};



}

You don't seem to be writing tickerTiers to the parcel. Something like this answer https://stackoverflow.com/a/10781119/156708 for parceling arrays of custom types.

Relevant code from other answer

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM