首页 技术 正文
技术 2022年11月11日
0 收藏 334 点赞 4,555 浏览 3649 个字

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()方法,否则返回的就不是数据内容,而是一长串数字。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297