帮助创建Parcelable类

时间:2011-09-28 11:17:53

标签: java android

我正在尝试创建一个Parcelable类。该类扩展了TableRow:

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.TableRow;

public class AcmTableRow extends TableRow implements Parcelable{

private int index;
public boolean isSection;
private String volumeLink;
private String rowId;
private String cfr;
private static Context context;
public AcmTableRow(Context context) {   

    super(context);

}
public AcmTableRow(Context context, Parcel in) {            
    super(context);
    AcmTableRow.context = context;
    readFromParcel(in);
}

private void readFromParcel(Parcel in) {

    //strValue = in.readString();
    //intValue = in.readInt();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    //dest.writeString(strValue);
    //dest.writeInt(intValue);
}


public void setIndex(int index) {
    this.index = index;
}

public int getIndex() {
    return index;
}

public void setRowId(String rowId) {
    this.rowId = rowId;
}

public String getRowId() {
    return rowId;
}

public void setCfr(String cfr) {
    this.cfr = cfr;
}

public String getCfr() {
    return cfr;
}

public void setVolumeLink(String volumeLink) {
    this.volumeLink = volumeLink;
}

public String getVolumeLink() {
    return volumeLink;
}   

public static final Parcelable.Creator<AcmTableRow> CREATOR = new Parcelable.Creator<AcmTableRow>() {
       public AcmTableRow createFromParcel(Parcel in) {            
           return new AcmTableRow(context, in);
       }
       public AcmTableRow[] newArray(int size) {
           return new AcmTableRow[size];
       }
   };

}

我很困惑,因为我需要投入:

readFromParcel(Parcel in)

writeToParcel(Parcel dest, int flags) 

非常感谢任何解释或帮助。

谢谢

菲尔

2 个答案:

答案 0 :(得分:1)

writeToParcel方法只是将对象展平为一个宗地。当您需要在活动之间传递对象时,您将使用它。在你的情况下,它应该是这样的:

public void writeToParcel (Parcel dest, int flags)
{
    dest.writeInt(index);
    dest.writeBoolean(isSection);
    dest.writeString(volumeLink);
    dest.writeString(rowId);
    dest.writeString(cfr);      

}

我从来没有使用过readFromParcel,我也不确定你是否需要这里。当您在下一个活动中调用适当的方法时,将从Parcel创建您的对象。

快速说明一下,您可能不希望在parcelable类中传递上下文。我甚至不确定你能不能。当您稍后再次膨胀parcelable时,您需要指定上下文。

答案 1 :(得分:0)

这是一个简单的工作Parcelable示例:

public class ParcelableExample implements Parcelable {
    private int intMember;
    private String stringMember;

    /*
     * Getters and setters
     * ....
     */
    /*
     * Constructors
     * ....
     */

     /*
      * Custom parcelable implementation
      */

     private ParcelableExample(Parcel in) {
         // notice that we are reading in the same order as we have written
         intMember = in.readInt();
         stringMember = in.readString();
     }

     @Override
     public int describeContents() {
         return hashCode();
     }

     @Override
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeInt(intMember);
         dest.writeString(stringMember);
     }

     @SuppressWarnings("rawtypes")
     public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
         public ParcelableExample createFromParcel(Parcel in) {
             return new ParcelableExample(in);
         }
         public ParcelableExample[] newArray(int size) {
             return new ParcelableExample[size];
         }
     }