Tu Hoang
Tu Hoang

Reputation: 4712

Android Parcelable Implementation with ArrayList<Object>

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

This is what I have so far:

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 */

}

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).

Upvotes: 7

Views: 16165

Answers (2)

user1525382
user1525382

Reputation:

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

Upvotes: 2

Nicholas Jordan
Nicholas Jordan

Reputation: 656

a lot of these things turn out to be R.P.C.'s -- I would write the major portion of the work in traditional Java then try to make the ......

yep, sure enough:

Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general Parcelable interface), and references to live IBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

in other words Parcel is a form of interprocess communication ....

you have to re-think the base model as this is not a 10,000 requests a second server - what it is is a hand-held consumer device that can just pull the pointers off the process table if it has to because of where it operates

I have learned to go to the blog tab at http://android-developers.blogspot.com/?hl=en

be very careful not to drill the entry deep into stuff that needs to be in worker threads and note the rich synchronization primitives that Java makes easy to use

Multithreading For Performance

Posted by Tim Bray on 19 July 2010 at 11:41 AM [This post is by Gilles Debunne, an engineer in the Android group who loves to get multitasked. — Tim Bray]

it's in the blogs --- much better place to start

Upvotes: 0

Related Questions