Help with creating a Parcelable class
I am trying to create a Parcelable class. The class extends 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];
开发者_StackOverflow中文版 }
};
}
I am confused as what I need to put in:
readFromParcel(Parcel in)
And
writeToParcel(Parcel dest, int flags)
Any explanation or help is greatly appreciated.
Thank you
Phil
The writeToParcel method simply flattens the object into a parcel. You'll use this when you need to pass your object between activities. In your case, it should look like this:
public void writeToParcel (Parcel dest, int flags)
{
dest.writeInt(index);
dest.writeBoolean(isSection);
dest.writeString(volumeLink);
dest.writeString(rowId);
dest.writeString(cfr);
}
I've never had to use readFromParcel and I'm not sure you need to here either. Your object will be created from the Parcel when you call the appropriate method in your next activity.
Just a quick note, you probably don't want to pass the context in your parcelable class. I'm not even sure you can. You'll need to assign the context when you inflate the parcelable again later on.
This is a simple working Parcelable example:
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];
}
}
精彩评论