Implementing parcelable in a class to pass it from an activity to another

1.2k views Asked by At

i have class student i tried to use seriablizable but couldnt make it work... this didnt work too can any one find whats wrong with it? that uses class semester and Course in its variables take a look

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

this is class Semester:

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];
    }
};}

This is class course

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

There are 1 answers

0
Emaster On BEST ANSWER

try this in Student class:

//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());
}

Do the same for the list private LinkedList current; in class Semester.