带有 ArrayList<Object> 的 Android Parcelable 实现 [英] Android Parcelable Implementation with ArrayList&lt;Object&gt;

查看:42
本文介绍了带有 ArrayList<Object> 的 Android Parcelable 实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在实现一个测试应用程序,我将在其中创建一个 Tournament 对象作为 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 的 Tournament 对象的规格,是否可以创建 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).

推荐答案

这里的代码向您展示了如何解析数组列表

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];
          }
    }; }

这篇关于带有 ArrayList<Object> 的 Android Parcelable 实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆