Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parcel List of List of Parcelable object

So I've parceled lists before, however I'm trying to parcel my GameBoard object, which actually has a List<List<Tile>>. Tile implements parcelable, but I'm not sure exactly how to parcel the List<List>. Here's what I've tried:

public class GameBoard implements Parcelable {
    private String _id;
    public String getId() { return _id; }
    public void setId(String id) { _id = id; }

    private List<List<Tile>> _tiles;
    public List<List<Tile>> getTiles() { return _tiles; }
    public void setTiles(List<List<Tile>> tiles) { _tiles = tiles; }

    public GameBoard(Parcel in) {
        _id = in.readString();
        in.readList(_tiles, ArrayList.class.getClassLoader());
    }

    public GameBoard() {

    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(_id);
        parcel.writeList(_tiles);
    }

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

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

The tile class correctly implements parcelable, I'm just not exactly sure how to read / write the List<List>> when parceling this class. Any ideas?

like image 788
Kevin DiTraglia Avatar asked May 31 '15 21:05

Kevin DiTraglia


1 Answers

Based on comments, I extended the ArrayList class with my custom class and implemented parcelable:

public class TileList extends ArrayList<Tile> implements Parcelable {

    public TileList(){
        super();
    }

    protected TileList(Parcel in) {
        in.readTypedList(this, Tile.CREATOR);
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeTypedList(this);
    }

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

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

and slight changes to my other class:

public class GameBoard implements Parcelable {
    private String _id;
    public String getId() { return _id; }
    public void setId(String id) { _id = id; }

    private List<TileList> _tiles;
    public List<TileList> getTiles() { return _tiles; }
    public void setTiles(List<TileList> tiles) { _tiles = tiles; }

    public GameBoard(Parcel in) {
        _id = in.readString();
        _tiles = new ArrayList<>();
        in.readTypedList(_tiles, TileList.CREATOR);
    }

    public GameBoard() {

    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(_id);
        parcel.writeTypedList(_tiles);
    }

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

        public GameBoard[] newArray(int size) {
            return new GameBoard[size];
        }
    };
}
like image 78
Kevin DiTraglia Avatar answered Oct 27 '22 10:10

Kevin DiTraglia