2013年5月19日 星期日

[Android] Passing an array list of user-defined objects to another activity

Passing objects between activities is common in Android. We pass strings, integers, arrays and etc. There are corresponding methods there. But what if we want to pass an array list of our own objects?

This time, I have an object include strings and integers received from a web service, and I want to pass it to main activity.

When passing an array list of user-defined objects between different activities on Andoird, using Parcelable is an efficient way to do it. To fulfill the requirement, we have to implement our object Parcelable and declare related functions.

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

沒有留言:

張貼留言