school类包含了一个major列表,可以增加该列表的元素,以及返回该列表,还实现了Parcelable.Creator接口。
package com.hzhi.my_sax;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class school implements Parcelable{
public static final String tag_name = "School";
public String name;
public String code;
public ArrayList<major> majors;
public static final Parcelable.Creator<school> CREATOR =
new Parcelable.Creator<school>(){
@Override
public school createFromParcel(Parcel in) {
// TODO Auto-generated method stub
return new school(in);
}
@Override
public school[] newArray(int size) {
// TODO Auto-generated method stub
return new school[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel arg0, int arg1) {
// TODO Auto-generated method stub
}
// 实现Parcelable接口
public school(Parcel in){
this.name = in.readString();
this.code = in.readString();
}
// 构造函数
public school(String name, String code){
this.name = name;
this.code = code;
this.majors = new ArrayList<major>();
}
// 增加专业
public void add_major(major m){
this.majors.add(m);
}
// 返回专业
public ArrayList<major> get_majors(){
return this.majors;
}
// 重写toString
public String toString() {
// TODO Auto-generated method stub
return (getName());
}
public String getName() {
if(this.name == null) {
return ("(Default)");
}
return (this.name);
}
}
major类包含了一个clas列表,可以增加该列表的元素,以及返回该列表,也实现了Parcelable.Creator接口。
package com.hzhi.my_sax;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class major implements Parcelable{
public static final String tag_name = "Major";
public String name;
public String code;
public ArrayList<clas> clases;
public static final Parcelable.Creator<major> CREATOR =
new Parcelable.Creator<major>(){
@Override
public major createFromParcel(Parcel in) {
// TODO Auto-generated method stub
return new major(in);
}
@Override
public major[] newArray(int size) {
// TODO Auto-generated method stub
return new major[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(this.name);
dest.writeString(this.code);
}
// 实现Parcelable接口
public major (Parcel in){
this.name = in.readString();
this.code = in.readString();
}
// 构造函数
public major(String name, String code){
this.name = name;
this.code = code;
this.clases = new ArrayList<clas>();
}
public String getCode() {
return (this.code);
}
public void add_clas(clas c) {
this.clases.add(c);
}
public ArrayList<clas> get_clases() {
return this.clases;
}
// 重写toString
public String toString() {
// TODO Auto-generated method stub
return (getName());
}
public String getName() {
if(this.name == null) {
return ("(Default)");
}
return (this.name);
}
}
clas类就不用包含列表了,但还是需要实现Parcelable.Creator接口。
package com.hzhi.my_sax;
import android.os.Parcel;
import android.os.Parcelable;
public class clas implements Parcelable {
public static final String tag_name = "Class";
public String name;
public String code;
public static final Parcelable.Creator<clas> CREATOR =
new Parcelable.Creator<clas>(){
@Override
public clas createFromParcel(Parcel in) {
// TODO Auto-generated method stub
return new clas(in);
}
@Override
public clas[] newArray(int size) {
// TODO Auto-generated method stub
return new clas[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(this.name);
dest.writeString(this.code);
}
public clas(Parcel in){
this.name = in.readString();
this.code = in.readString();
}
public clas (final String name, final String code){
this.name = name;
this.code = code;
}
// 重写toString
public String toString() {
// TODO Auto-generated method stub
return (getName());
}
public String getName() {
if(this.name == null) {
return ("(Default)");
}
return (this.name);
}
}
以上4个类中,tag_name是为了和XML文件中的节点名称对比,name和code分别对应XML文件中的name和code。4个类都要重写toString()方法,否则返回的就不是数据内容,而是一长串数字。