Here is an example, we have a class called TrackDetail with integer and string attributes implements Parcelable and add related functions in the code.
public class TrackDetail implements Parcelable
{
private int discNumber;
private int trackNumber;
private String composerName;
private String workName;
private String titleName;
public TrackDetail(int discno, int trackno, String comNm, String wkNm, String tNm)
{
this.discNumber = discno;
this.trackNumber = trackno;
this.composerName = comNm;
this.workName = wkNm;
this.titleName = tNm;
}
public TrackDetail(Parcel in) {
readFromParcel(in);
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(discNumber);
dest.writeInt(trackNumber);
dest.writeString(composerName);
dest.writeString(workName);
dest.writeString(titleName);
}
private void readFromParcel(Parcel in) {
// We just need to read back each
// field in the order that it was
// written to the parcel
discNumber = in.readInt();
trackNumber = in.readInt();
composerName = in.readString();
workName = in.readString();
titleName = in.readString();
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public TrackDetail createFromParcel(Parcel in) {
return new TrackDetail(in);
}
public TrackDetail[] newArray(int size) {
return new TrackDetail[size];
}
};
}
Then we can just ignore the functions and use it as a normal class.
ArrayList<TrackDetail> tdList = new ArrayList<TrackDetail>();
for (int i = 0; i< count; i++)
{
SoapObject tmp = (SoapObject)resultAry.getProperty(i);
TrackDetail td = new TrackDetail(
Integer.parseInt(tmp.getProperty("discNumber").toString()),
Integer.parseInt(tmp.getProperty("trackNumber").toString()),
tmp.getProperty("composerName").toString(),
tmp.getProperty("workName").toString(),
tmp.getProperty("titleName").toString());
tdList.add(td);
}
Finally we can use the putParcelableArrayList method of bundle to pass it.
Bundle b = new Bundle(); //Bundle will take the results
b.putInt("result", count); //add to bundle
b.putParcelableArrayList("tdlist", tdList);
//pass the array list of track detail to main activity
receiver.send(0, b);
沒有留言:
張貼留言