0

In my app I have a class CurrentGame, which I set up according to the example in this question: Android Class Parcelable with ArrayList

However, I don't know how to add the intArray scoresRnd to the Parcel Constructor.

When I add this line, for example, it gets underlined in red:

this.scoresRnd = parcel.readArray(null);

...and I get this error message "Incompatible types. Required: Int[]. Found: java.lang.Object[]". Can you please help me? Thanks! (Also, please point out any other mistakes in my code)

public class CurrentGame implements Parcelable{
    private int rnd;
    private int[] scoresRnd;
    private ArrayList<int[]> scoreList;

    public CurrentGame(){
        super();
        this.rnd = 0;
        this.scoresRnd = new int[]{0,0,0,0};
        this.scoreList = new ArrayList<>();
    }

    public CurrentGame (Parcel parcel){
        this.scoreList = parcel.readArrayList(null);
        this.rnd = parcel.readInt();
        //WHAT DO I PUT HERE FOR THE INTARRAY???

    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        // TODO Auto-generated method stub
        parcel.writeList(this.scoreList);
        parcel.writeInt(this.rnd);
    }

    public static final Creator<CurrentGame> CREATOR = new Creator<CurrentGame>() {

        @Override
        public CurrentGame createFromParcel(Parcel parcel) {
            // TODO Auto-generated method stub
            return new CurrentGame(parcel);
        }

        @Override
        public CurrentGame[] newArray(int i) {
            // TODO Auto-generated method stub
            return new CurrentGame[i];
        }
    };

    public void expandList(int[] scoresRnd){
        scoreList.add(scoresRnd);
    }

    public void setRound(int rnd){
        this.rnd=rnd;
    }

    public int getRound(){
        return rnd;
    }

    public ArrayList<int[]> getScoreList(){
        return scoreList;
    }
}
Kubi
  • 73
  • 6

1 Answers1

0

Try this

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.rnd);
    dest.writeIntArray(this.scoresRnd);
    dest.writeList(this.scoreList);
}

public s() {
}

protected s(Parcel in) {
    this.rnd = in.readInt();
    this.scoresRnd = in.createIntArray();
    this.scoreList = new ArrayList<int[]>();
    in.readList(this.scoreList, int[].class.getClassLoader());
}

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

    @Override
    public s[] newArray(int size) {
        return new s[size];
    }
};
Sunny
  • 14,522
  • 15
  • 84
  • 129
  • thanks, I'll try it! But is there also a way which doesn't alter my existing code too much? Or is it my code the wrong approach? – Kubi Nov 13 '17 at 09:02
  • `dest.writeIntArray(this.scoresRnd);` is what you required. If you need that to auto generate that there are lots of plugin for that in Android Studio. So you don't need to worry about `Parcelable ` – Sunny Nov 13 '17 at 09:42
  • Ok, so I put 'dest.writeIntArray(this.scoresRnd);' into the 'writeToParcel'-method. But what do I add to my Parcel constructor? – Kubi Nov 13 '17 at 09:55
  • You don't need anything there. Make it as it is. Or make it empty. It's your choice. – Sunny Nov 13 '17 at 09:58