java - Parcel List of List of Parcelable object -
so i've parceled lists before, i'm trying parcel gameboard
object, has list<list<tile>>
. tile
implements parcelable, i'm not sure how parcel list<list>
. here's 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 not sure how read / write list<list>>
when parceling class. ideas?
based on comments, extended arraylist
class custom class , 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 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]; } }; }
Comments
Post a Comment