Android使用SAX解析XML(2)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值