在类中实现parcelable以将其从活动传递到另一个

时间:2013-12-04 09:26:38

标签: android object android-intent parcelable parcel

我有班级学生 我试图使用seriablizable但无法使它工作...这也没有工作也可以找到什么错误吗? 在其变量中使用课程学期和课程看看

public class Student implements Parcelable {
private double GPA, MGPA;
private LinkedList<Course> finishedcourses;
private String username, password;
private Semester semester;


public Student(String user, String pass, double gpa, double mgpa,
        LinkedList<Course> fc, Semester s) {
    username = user;
    password = pass;
    GPA = gpa;
    MGPA = mgpa;
    semester = s;
    finishedcourses = fc;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeString(username);
    out.writeString(password);
    out.writeDouble(GPA);
    out.writeDouble(MGPA);
    out.writeParcelable((Parcelable) finishedcourses , flags);
    out.writeParcelable((Parcelable) semester, flags);

}


public static final Creator<Student> CREATOR = new Parcelable.Creator<Student> (){
    public Student createFromParcel(Parcel in) {
        return new Student(in); 
    }

    public Student[] newArray(int size) {
        return new Student[size];
    }
};


public Student(Parcel in)
{
    username = in.readString();
    password = in.readString();
    GPA = in.readDouble();
    MGPA = in.readDouble();
    finishedcourses = in.readParcelable(Course.class.getClassLoader());
    semester = in.readParcelable(Semester.class.getClassLoader());
}}

这是课程学期:

public class Semester implements Parcelable{
private LinkedList<Course> current;
private int Credits;

public Semester(LinkedList<Course> current) {
    super();
    this.current = current;
    Credits = calcCredits();
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(Credits);
    out.writeParcelable((Parcelable) current, flags);

}


public Semester(Parcel in) {
    Credits = in.readInt();
    current = in.readParcelable(Course.class.getClassLoader());
}


public static final Creator<Semester> CREATOR = new Parcelable.Creator<Semester>() {
    public Semester createFromParcel(Parcel in)
    {
        return new Semester(in);
    }

    @Override
    public Semester[] newArray(int size) {
        // TODO Auto-generated method stub
        return new Semester[size];
    }
};}

这是课程

public class Course implements Parcelable{  
private int credits;
private String LetterGrade,Name;

public Course(int credits, String Name, String Letter) {
this.Name = Name;
LetterGrade = Letter;
this.credits = credits;
}


@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeString(Name);
    out.writeString(LetterGrade);
    out.writeInt(credits);
}
public static final Creator<Course> CREATOR = new Parcelable.Creator<Course>() {

    @Override
    public Course createFromParcel(Parcel in) {
        return new Course(in);
    }

    @Override
    public Course[] newArray(int size) {

        return new Course[size];
    }

};
public Course (Parcel in){
    Name = in.readString();
    LetterGrade = in.readString();
    credits = in.readInt();
}}

1 个答案:

答案 0 :(得分:3)

在学生班中尝试:

//initialize the list
private LinkedList<Course> finishedcourses = new LinkedList<Course>();

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeString(username);
    out.writeString(password);
    out.writeDouble(GPA);
    out.writeDouble(MGPA);
    //replace by writeTypedList
    out.writeTypedList(finishedcourses);
    out.writeParcelable(semester, flags);
}


public Student(Parcel in)
{
    username = in.readString();
    password = in.readString();
    GPA = in.readDouble();
    MGPA = in.readDouble();
    //replace by readTypedList
    in.readTypedList(finishedcourses, Course.CREATOR);
    semester = in.readParcelable(Semester.class.getClassLoader());
}

对于课程学期中的列表private LinkedList current;执行相同的操作。