问题描述
让我实现一个测试程序中,我将创建一个比赛对象Parcelable,并通过他们的意图之间。一个赛事包括:。 ?联赛名称。规则。规则匹配的球员(随机/手动)。玩家的数组列表
so I am implementing a test app in which I will create a Tournament object as Parcelable and will pass them between intents.A tournament include:. A tournament name. Rule. Rule for matching players (random/manual). An array list of Players
这是我到目前为止有:
Tournament.java
public class TournamentData implements Parcelable {
private String tourName;
private int bestOf;
private boolean isRandom;
private ArrayList<Player> playerList;
public TournamentData(String name, int tourBestOf, boolean random) {
this.tourName = name;
this.bestOf = tourBestOf;
this.isRandom = random;
}
public void addPlayer(Player newPlayer) {
this.playerList.add(newPlayer);
}
public ArrayList<Player> getPlayerList() {
return playerList;
}
/* getters and setters excluded from code here */
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel out, int flags) {
// TODO Auto-generated method stub
}
Player.java
public class Player {
private String playerName;
private String playerEmail;
public Player(String name, String email) {
this.playerName = name;
this.playerEmail = email;
}
/* getter and setters are excluded */
}
我是新来的Android(我的意思是非常非常新的10小时后它,我猜)。所以我想知道:。是否有可能创建因为有比赛的ArrayList对象的规范一个Parcelable对象?。如何所有的比赛数据存储到一个Parcelable对象,然后从其他活动访问它们? (即A和B)。
I am new to Android (i mean very very new; 10 hours into it I guess). So I am wondering:. Is it possible to create a Parcelable object given the specs of Tournament object that has ArrayList?. How to store all the tournament data into a Parcelable object and access them from the other activity? (namely A and B).
推荐答案
这里是code,它告诉你如何parcle一个的ArrayList
here is code that show you how parcle a arraylist
public class ParcleListTopic implements Parcelable{
private List<ParcleTopic> list;
private ArrayList<HoldListTopic> listh=new ArrayList<HoldListTopic>();
public ArrayList<HoldListTopic> GetListTopic()
{
for(int i=0;i<list.size();i++)
{
listh.add(list.get(i).GetHold());
}
return listh;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(list);
}
public ParcleListTopic(Parcel in)
{
in.readTypedList(list,ParcleTopic.CREATOR);
}
public ParcleListTopic(List<ParcleTopic> list)
{
this.list=list;
}
public static final Parcelable.Creator<ParcleListTopic> CREATOR = new Parcelable.Creator<ParcleListTopic>(){
public ParcleListTopic createFromParcel(Parcel s)
{
return new ParcleListTopic(s);
}
public ParcleListTopic[] newArray(int size)
{
return new ParcleListTopic[size];
}
};
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
public class ParcleTopic implements Parcelable
{
HoldListTopic hold;
public ParcleTopic(HoldListTopic hold)
{
this.hold=hold;
}
public HoldListTopic GetHold()
{
return hold;
}
public int describeContents()
{
return 0;
}
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(hold.Title);
dest.writeString(hold.Date);
dest.writeInt(hold.NumberComment);
dest.writeInt(hold.ID);
}
public ParcleTopic(Parcel in)
{
hold.Title=in.readString();
hold.Date=in.readString();
hold.NumberComment=in.readInt();
hold.ID=in.readInt();
}
public static final Parcelable.Creator<ParcleTopic> CREATOR = new Parcelable.Creator<ParcleTopic>()
{
public ParcleTopic createFromParcel(Parcel s)
{
return new ParcleTopic(s);
}
public ParcleTopic[] newArray(int size)
{
return new ParcleTopic[size];
}
}; }
这篇关于Android的Parcelable实现与ArrayList的&LT;对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!