Parcelable 对象列表的包裹列表
Parcel List of List of Parcelable object
所以我之前已经打包过列表,但是我正在尝试打包我的 GameBoard
对象,它实际上有一个 List<List<Tile>>
。 Tile
实现可打包,但我不确定如何打包 List<List>
。这是我尝试过的:
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];
}
};
图块 class 正确地实现了 parcelable,我只是不确定在打包这个 class 时如何读/写 List<List>>
。有什么想法吗?
根据评论,我用我的自定义 class 扩展了 ArrayList
class 并实现了 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];
}
};
}
并对我的其他 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];
}
};
}
所以我之前已经打包过列表,但是我正在尝试打包我的 GameBoard
对象,它实际上有一个 List<List<Tile>>
。 Tile
实现可打包,但我不确定如何打包 List<List>
。这是我尝试过的:
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];
}
};
图块 class 正确地实现了 parcelable,我只是不确定在打包这个 class 时如何读/写 List<List>>
。有什么想法吗?
根据评论,我用我的自定义 class 扩展了 ArrayList
class 并实现了 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];
}
};
}
并对我的其他 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];
}
};
}