主界面MainActivity.java:
package com.hzhi.my_sax;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements OnItemSelectedListener{
public static final String str_school = "School";
public static final String str_major = "Major";
public static final String str_clas = "Class";
// 控件变量
public Spinner spn_school;
public Spinner spn_major;
public Spinner spn_clas;
public infor m_infor;
// 适配器容器
public ArrayList<school> arr_school = new ArrayList<school>();
public ArrayList<major> arr_major = new ArrayList<major>();
public ArrayList<clas> arr_clas = new ArrayList<clas>();
// 选择的项目编号
public int pos_school;
public int pos_major;
public int pos_class;
// 选择班级次数
public int c_num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得控件
get_con();
// 设置适配器
ArrayAdapter<school> ada_school = new ArrayAdapter<school>(this,
android.R.layout.simple_spinner_item, arr_school);
ada_school.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spn_school.setAdapter(ada_school);
ArrayAdapter<major> ada_major = new ArrayAdapter<major>(this,
android.R.layout.simple_spinner_item, arr_major);
ada_major.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spn_major.setAdapter(ada_major);
ArrayAdapter<clas> ada_clas = new ArrayAdapter<clas>(this,
android.R.layout.simple_spinner_item, arr_clas);
ada_clas.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spn_clas.setAdapter(ada_clas);
// 设置监听器
spn_school.setOnItemSelectedListener(this);
spn_major.setOnItemSelectedListener(this);
spn_clas.setOnItemSelectedListener(this);
m_infor = new infor();
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// 获得控件
public void get_con(){
spn_school = (Spinner) findViewById(R.id.spn_school);
spn_major = (Spinner) findViewById(R.id.spn_major);
spn_clas = (Spinner) findViewById(R.id.spn_class);
}
// 选择学院
public void do_school_select() {
//清除数据集
arr_major.clear();
arr_clas.clear();
//设置数据集
arr_major.addAll(m_infor.get_schools().get(pos_school).get_majors());
arr_clas.addAll(
m_infor.get_schools().get(pos_school).get_majors().get(0).get_clases());
//通知数据集改变
((ArrayAdapter)this.spn_major.getAdapter()).notifyDataSetChanged();
((ArrayAdapter)this.spn_clas.getAdapter()).notifyDataSetChanged();
//初始化设置选择项
this.spn_major.setSelection(0);
this.spn_clas.setSelection(0);
c_num = 0;
}
// 选择专业
public void do_major_select() {
//清除数据集
arr_clas.clear();
//设置数据集
arr_clas.addAll(
m_infor.get_schools().get(pos_school).get_majors().get(pos_major).get_clases());
//通知数据集改变
((ArrayAdapter)spn_clas.getAdapter()).notifyDataSetChanged();
//初始化设置选择项
spn_clas.setSelection(0);
c_num = 0;
}
// 选择班级
public void do_class_select(){
if (c_num>0)
{
school b_school = m_infor.get_schools().get(pos_school);
major b_major = b_school.get_majors().get(pos_major);
clas b_clas = b_major.get_clases().get(pos_class);
Intent i = new Intent(this, DetailActivity.class);
i.putExtra(str_school, b_school);
i.putExtra(str_major, b_major);
i.putExtra(str_clas, b_clas);
this.startActivity(i);
}
c_num++;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// TODO Auto-generated method stub
switch(parent.getId()) {
case R.id.spn_school: {
pos_school = pos;
do_school_select();
break;
}
case R.id.spn_major: {
pos_major = pos;
do_major_select();
break;
}
case R.id.spn_class: {
pos_class = pos;
do_class_select();
break;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
// 清除数据集
public void clear_all_dataset(){
arr_school.clear();
arr_major.clear();
arr_clas.clear();
}
// 通知所有数据集已经改变
public void notify_all_dataset(){
((ArrayAdapter) spn_school.getAdapter()).notifyDataSetChanged();
((ArrayAdapter) spn_major.getAdapter()).notifyDataSetChanged();
((ArrayAdapter) spn_clas.getAdapter()).notifyDataSetChanged();
}
// 设置数据集
public void add_all_dataset(){
arr_school.addAll(m_infor.get_schools());
arr_major.addAll(m_infor.get_schools().get(0).get_majors());
arr_clas.addAll(m_infor.get_schools().get(0).get_majors().
get(0).get_clases());
}
// 初始化界面
public void init(){
super.onStart();
util.get_ins().parse(
getResources().openRawResource(R.raw.infor),m_infor);
// 清除数据集
clear_all_dataset();
// 设置数据集
add_all_dataset();
// 通知数据集改变
notify_all_dataset();
}
}
最重要的语句是init()函数里面的
util.get_ins().parse(getResources().openRawResource(R.raw.infor),m_infor);
这条语句开始了解析infor.xml文件的过程,并将解析的结果放在变量m_infor里面。