0

I am attempting to pass an array List of Custom objects through from one class to the other. I used a wrapper class to pass over the array List:

package perks;

import java.io.Serializable;
import java.util.ArrayList;

public class PerkWrapper implements Serializable {

   private ArrayList<Perk> parliaments;

   public PerkWrapper(ArrayList<Perk> perks) {
      this.parliaments = perks;
   }

   public ArrayList<Perk> getParliaments() {
      return this.parliaments;
   }

}

I pass it like this:

i.putExtra("perks", player.perks); 

Where player.perks is the arrayList containing teh Perk object And i retrieve it like so:

PerkWrapper pw = (PerkWrapper) getIntent().getSerializableExtra("perks");
        plrPerks = pw.getParliaments();
        player.perks = plrPerks;

When i run the app, i get the following error:

Unable to start activity.. ClassCastException ArrayList cannot be cast to   
perks.perkwrapper

Here is my Perk class: (The object in the array List):

package perks;
public class Perk implements Parcelable {
public String name;
public String desc;
public int cost;

public int roundReq;
public int rankReq;

public int minusDec;
public int plusInc;
public int autoClick;
public int rewardBonus;

public Perk() {
    this.name = "";
    this.desc = "";
    this.cost = 0;
    this.roundReq = 1;
    this.rankReq = 1;
    this.minusDec = 1;
    this.plusInc = 1;
    this.autoClick = 1;
    this.rewardBonus = 1;
}

public Perk(Parcel in) {
    name = in.readString();
    desc = in.readString();
    cost = in.readInt();
    roundReq = in.readInt();
    rankReq = in.readInt();
    minusDec = in.readInt();
    plusInc = in.readInt();
    autoClick = in.readInt();
    rewardBonus = in.readInt();
}

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

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(desc);
    dest.writeInt(cost);
    dest.writeInt(roundReq);
    dest.writeInt(rankReq);
    dest.writeInt(minusDec);
    dest.writeInt(plusInc);
    dest.writeInt(autoClick);
    dest.writeInt(rewardBonus);
}

}

How can i prevent this error from happening or are there any alternatives to passing array Lists simply? Thank you for your time

3 Answers3

1

You are passing player.perks which is an ArrayList of Perk and in your other activity you are getting PerkWrapper and hence it is giving you error.You will need to do something like this

ArrayList<Perk> perks = (ArrayList<Perk>) getIntent().getSerializableExtra("perks");
Apoorv
  • 13,470
  • 4
  • 27
  • 33
0

Reply to @amit singh's Answer (My reputation not yet upto comment level)

The method specified is possible possible only for arraylist of the format ArrayList. For custom objects, this is not applicable.

RmK
  • 1,408
  • 15
  • 28
-1

To pass arraylist between activities, use-

Intent intent = new Intent(this, secondActivity.class);
        intent.putStringArrayListExtra("list", samplelist);
        startActivity(intent);

In your receiving intent you need to do:

Intent i = getIntent();  
stock_list = i.getStringArrayListExtra("list");
amit singh
  • 1,407
  • 2
  • 16
  • 25