Android中实现带有ArrayList<Object>的Parcelable功能

7

我正在实现一个测试应用程序,其中我将创建一个可传递的Tournament对象,并在不同Intent之间传递它们。

Tournament对象包括:

  • 比赛名称
  • 规则
  • 匹配玩家的规则(随机/手动)
  • 球员数组列表

以下是我目前的代码:

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个小时)。所以我想知道: - 是否可以根据Tournament对象的规格创建一个具有ArrayList的Parcelable对象? - 如何将所有比赛数据存储到Parcelable对象中并从其他活动(即A和B)中访问它们?


2
请查看此答案,其中包含有关如何使用Parcelable解析arrayList的完整示例。 https://dev59.com/eOo6XIcBkEYKwwoYSCg1#7400675 - Lalit Poptani
@LalitPoptani:感谢您的帮助。我已经完成了可包含对象的实现!我很快会添加我所实现的代码,以便其他人在遇到相同问题时可以参考它! - Tu Hoang
好的,谢谢。我已经发布了我的答案。 :) - Lalit Poptani
将它们在意图之间传递。 - Henadzi Rabkin
试试这个工具,非常有用:http://devk.it/proj/parcelabler/ - Jumpa
显示剩余2条评论
2个回答

2
这里有一段代码展示如何对ArrayList进行打包(parcle):
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];
          }
    }; }

0

很多时候这些东西都会变成R.P.C.——我会用传统的Java编写大部分工作,然后尝试将其转换为......

没错,确实是这样:

IBinder可以通过它发送包含消息(数据和对象引用)的容器。Parcel既可以包含将在IPC的另一侧上解压缩的扁平化数据(使用此处的各种方法来编写特定类型或通用Parcelable接口),也可以包含对活动IBinder对象的引用,这将导致另一侧接收到与Parcel中原始IBinder连接的代理IBinder。

换句话说,Parcel是一种进程间通信的形式......

你必须重新思考基本模型,因为这不是一个每秒10,000个请求的服务器——它是一个手持消费设备,如果必要,它只能从进程表中提取指针,因为它的操作位置。

我已经学会了去http://android-developers.blogspot.com/?hl=en的博客标签页。

非常小心,不要深入到需要在工作线程中运行的内容,并注意Java使使用丰富的同步原语变得容易。

多线程提高性能
发布者:Tim Bray,发布时间:2010年7月19日上午11:41 [本文由Android团队的工程师Gilles Debunne撰写,他热衷于多任务处理。——Tim Bray]
博客是一个更好的起点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接