android学生管理系统,利用linerLayout,实现xml的读取和写入,以及按钮的事件处理


详细解说都在代码里

在这里我只展示,几个类的主要代码

还是一句老话,注意写入权限的使用


操作类

package com.leige.stu.op;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.WriteAbortedException;
import java.util.ArrayList;
import java.util.List;


import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;


import android.os.Environment;
import android.util.Log;
import android.util.Xml;


import com.leige.stu.domain.Student;


public class StudentDaoimpl implements StudentDao{
//保存到sd卡中




@Override
public void save(List list){
//获取解析器

//解析范本
/*
* 
* 
* 战三
* 
* 18
* 
* 
* */
XmlSerializer write=Xml.newSerializer();
File path=new File(Environment.getExternalStorageDirectory(),"students.xml");
try {
//实例化输出流
FileOutputStream out=new FileOutputStream(path);
//解析器绑定输出流,设置编码
write.setOutput(out, "utf-8");
//开始写xml
write.startDocument("utf-8",true);
//开始根节点
write.startTag(null, "students");
//循环写入student
for(Student stu:list){
//子节点student
write.startTag(null, "student");
//设置id
write.attribute(null, "id", String.valueOf(stu.getId()));
   write.startTag(null, "name");//name
   write.text(stu.getName());
   write.endTag(null, "name");
   
 //age
   write.startTag(null, "age");
   write.text(String.valueOf(stu.getAge()));
   write.endTag(null, "age");
   
   
   //sex
   write.startTag(null, "sex");
   write.text(stu.getSex());
   write.endTag(null, "sex");
   
write.endTag(null, "student");

}





write.endTag(null, "students");
//结束
write.endDocument();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("err",e.toString());
}

}
//读取


//解析范本
/*
* 
* 
* 战三
* 
* 18
* 
* 
* */
@Override
public List getStuList(){
//获取读取解析器
XmlPullParser read=Xml.newPullParser();
File path=new File(Environment.getExternalStorageDirectory(),"students.xml");
List students=null;
Student student=null;
try {
FileInputStream input=new FileInputStream(path);
read.setInput(input,"utf-8");
//获取时间类型
int eventType=read.getEventType();
//设置循环读取
while(eventType!=XmlPullParser.END_DOCUMENT){
//获取当前节点,名字
String eleName=read.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
{
if(eleName.equals("students")){//students
//建立集合
students=new ArrayList();
}else if(eleName.equals("student")){
//实例化对象
student=new Student();
//设置id属性
student.setId(Integer.parseInt(read.getAttributeValue(null, "id")));

}else if(eleName.equals("name")){//name
//设置name
student.setName(read.nextText());
}else if(eleName.equals("age")){//age
student.setAge(Integer.parseInt(read.nextText()));
}else if(eleName.equals("sex")){//sex
student.setSex(read.nextText());
}

}

break;


case XmlPullParser.END_TAG:
{
if(eleName.equals("student")){
//加入集合
students.add(student);
}else if(eleName.equals("students")){
return students;
}
}
break;
}
eventType=read.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



return null;

}

}


主活动类

package com.leige.stu;


import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.leige.stu.domain.Student;
import com.leige.stu.op.StudentDao;
import com.leige.stu.op.StudentDaoimpl;


public class MainActivity extends Activity implements OnClickListener{
	private LinearLayout show=null;
	private Button add=null;
	private EditText name=null;
	private EditText age=null;
	private Spinner sex=null;
	private String sexValue;
	String[] arr;
	int count=0;
	//声明操作类
	StudentDao dao=new StudentDaoimpl();
	//声明保存集合
	List students=new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化布局对象
        show=(LinearLayout) findViewById(R.id.show);
        add=(Button) findViewById(R.id.add);
        name=(EditText) findViewById(R.id.name);
        age=(EditText) findViewById(R.id.age);
        sex= (Spinner) findViewById(R.id.sex);
        arr=new String[]{"男","女"};
        //设置下拉菜单内容
        ArrayAdapter adapter=new ArrayAdapter(this,
        		android.R.layout.simple_spinner_item,arr);
 
      //设置下拉列表的风格
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       //设置适配器
       sex.setAdapter(adapter);
       sex.setVisibility(View.VISIBLE);
       sex.setOnItemSelectedListener(new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			sexValue=arr[position];
		}

		@Override
		public void onNothingSelected(AdapterView parent) {
			// TODO Auto-generated method stub
			
		}

	
	});
       for(Student stu:dao.getStuList()){
    	   addView(stu);
    	   students.add(stu);
    	   
       }
     
    }
    //声明对象
	Student student;


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.add:
		{
				String stuName=name.getText().toString();
				if(stuName==""||stuName.trim().length()<=0){
					Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
					break;
				}
				int stuAge = 0;
				try {
					stuAge=Integer.parseInt(age.getText().toString());
				} catch (Exception e) {
					Toast.makeText(this, "年龄输入不正确", Toast.LENGTH_SHORT).show();
					break;
				}
				//实例化对象
				student=new Student();
				
				//设置对象
			    student.setAge(stuAge);
				student.setId(count++);
				student.setSex(sexValue);
				student.setName(stuName);
				
				//将students实例化,并把student加入集合
				
				students.add(student);
				addView(student);
				//清空编辑文本
				name.setText("");
				age.setText("");
				
		}	
			break;

			case R.id.save:
			{
				if(students!=null){
					dao.save(students);
					Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
				}
			}
			break;
			
			
			case R.id.cancel:
			{
				Toast.makeText(this, "取消保存", Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}
	private void addView(Student stu) {
		// TODO Auto-generated method stub
		//加载显示文件布局,建议放在外面,因为不必要每次都加在布局文件
		View view=LayoutInflater.from(this).inflate(R.layout.item, null);
		TextView show_name=(TextView) view.findViewById(R.id.show_name);
		
		TextView show_age=(TextView) view.findViewById(R.id.show_age);
		TextView show_sex=(TextView) view.findViewById(R.id.show_sex);
		//设置显示文本
		show_name.setText(stu.getName());
		show_age.setText(String.valueOf(stu.getAge()));
		show_sex.setText(stu.getSex());
		show.addView(view);
	}

  
}


 
  
 
  
布局效果图

 
  
 
  


android学生管理系统,利用linerLayout,实现xml的读取和写入,以及按钮的事件处理_第1张图片

你可能感兴趣的:(android)