简体   繁体   English

在类中实现可包裹性以将其从活动传递到另一个活动

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

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

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; 对列表private LinkedList current;列表执行相同的操作private LinkedList current; in class Semester. 在上学期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将实现Parcelable的对象从一个活动传递到另一个活动 - Pass Object implementing Parcelable from one Activity to Another Activity 如何通过实现可拆分界面从一个活动到另一个活动来传递图像数据? - How to pass an image data by implementing parcelable interface from one activity to another? 将包裹类从片段传递到另一个活动 - passing parcelable class from fragment to another activity 如何将非parcelable对象从活动传递到另一个活动? - How to pass non parcelable objects to from activity to another activity? 如何使用parcelable在活动之间传递线程类对象 - how to pass a thread class object from activity to activity using parcelable 使用 Parcelable 将对象从一个 android 活动传递到另一个 - Use Parcelable to pass an object from one android activity to another 无法将作为putExtra的Parcelable对象从IntentService传递到另一个Activity - Not ablet to pass Parcelable object as putExtra from IntentService to another Activity 尝试将实现可拆分的自定义对象传递给使用Intent的另一个活动时发生ClassCastException - ClassCastException when trying to pass custom object implementing parcelable to another activity using Intent 如何将Parcelable Extra传递给另一个活动 - How to pass a Parcelable Extra to another activity 将对象传递到另一个活动(没有Parcelable?) - Pass object to another activity (without Parcelable?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM