博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的学生管理系统
阅读量:4934 次
发布时间:2019-06-11

本文共 7232 字,大约阅读时间需要 24 分钟。

简单的学生管理系统

一,主要功能:

  1,添加学生信息;

  2,添加的学生信息显示在线型布局中;

  3,把学生信息保存在xml文件中;

  4,把保存在mxl中的学生信息取出来解析显示在界面;

二,主要知识点:

  1,layout_weight的使用;

  2,pull解析xml文件,xml序列化;

  3,保存数据到sd卡;

  4,动态添加控件刷新界面;

三,界面原型如下图:

四,代码展示:

  1,界面xml文件,activity_main.xml

View Code

  2,定义实体类,Student.java

public class Student {    private String name;    private String sex;    private Integer age;    public Student(String name, String sex, Integer age) {        super();        this.name = name;        this.sex = sex;        this.age = age;    }    public Student() {        super();        // TODO Auto-generated constructor stub    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    @Override    public String toString() {        return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";    }}
View Code

  3,主界面activity,MainActivity.java

public class MainActivity extends Activity implements OnClickListener {    private EditText etName;    private EditText etSex;    private EditText etAge;    private LinearLayout llStudentList;    private List
studentList; private String filePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { etName = (EditText) findViewById(R.id.et_name); etSex = (EditText) findViewById(R.id.et_sex); etAge = (EditText) findViewById(R.id.et_age); llStudentList = (LinearLayout) findViewById(R.id.ll_student_list); findViewById(R.id.btn_save).setOnClickListener(this); findViewById(R.id.btn_restore).setOnClickListener(this); findViewById(R.id.btn_add_student).setOnClickListener(this); studentList = new ArrayList
(); filePath = Environment.getExternalStorageDirectory().getPath() + "/student.xml"; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_save: if(studentList.size() > 0) { if(saveStudent2Local()) { Toast.makeText(this, "保存成功", 0).show(); } else { Toast.makeText(this, "保存失败", 0).show(); } } else { Toast.makeText(this, "当前没有数据", 0).show(); } break; case R.id.btn_restore: if(restoreStudentFromLocal()) { Toast.makeText(this, "恢复成功", 0).show(); } else { Toast.makeText(this, "恢复失败", 0).show(); } break; case R.id.btn_add_student: addStudent(); break; default: break; } } private boolean restoreStudentFromLocal() { try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(new FileInputStream(filePath), "utf-8"); int eventType = parser.getEventType(); studentList.clear(); Student student = null; String nodeName = null; while(eventType != XmlPullParser.END_DOCUMENT) { nodeName = parser.getName(); switch (eventType) { case XmlPullParser.START_TAG: if("student".equals(nodeName)) { student = new Student(); } else if("name".equals(nodeName)) { student.setName(parser.nextText()); } else if("sex".equals(nodeName)) { student.setSex(parser.nextText()); } else if("age".equals(nodeName)) { student.setAge(Integer.valueOf(parser.nextText())); } break; case XmlPullParser.END_TAG: if("student".equals(nodeName)) { studentList.add(student); } break; default: break; } eventType = parser.next(); } refreshStudentList(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } private void refreshStudentList() { llStudentList.removeAllViews(); TextView childView; for (Student student : studentList) { childView = new TextView(this); childView.setTextSize(23); childView.setTextColor(Color.BLACK); childView.setText("  " + student.getName() + "  " + student.getSex() + "  " + student.getAge()); llStudentList.addView(childView); } } private boolean saveStudent2Local() { try { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(new FileOutputStream(filePath), "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "infos"); for (Student stu : studentList) { serializer.startTag(null, "student"); serializer.startTag(null, "name"); serializer.text(stu.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "sex"); serializer.text(stu.getSex()); serializer.endTag(null, "sex"); serializer.startTag(null, "age"); serializer.text(String.valueOf(stu.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "student"); } serializer.endTag(null, "infos"); serializer.endDocument(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } private void addStudent() { String name = etName.getText().toString(); String sex = etSex.getText().toString(); String age = etAge.getText().toString(); if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(sex) && !TextUtils.isEmpty(age)) { studentList.add(new Student(name, sex, Integer.valueOf(age))); TextView childView = new TextView(this); childView.setTextSize(23); childView.setTextColor(Color.BLACK); childView.setText("  " + name + "  " + sex + "  " + age); llStudentList.addView(childView); } else { Toast.makeText(this, "请正确输入", 0).show(); } }}
View Code

 

通过以上步骤基本实现功能,不过还有需要完善的地方,比如年龄校验等

  

转载于:https://www.cnblogs.com/wk843620202/p/4404534.html

你可能感兴趣的文章
WF+WCF+WPF第二天--模拟超市收银
查看>>
爬取贴吧好看的桌面图片 -《狗嗨默示录》-
查看>>
[转]这13个开源GIS软件,你了解几个?
查看>>
Shell批量启动、关闭tomcat
查看>>
C++成员函数的重载、覆盖与隐藏【转载】
查看>>
网站开发技能图谱
查看>>
4.27随笔
查看>>
CSS实例:图片导航块
查看>>
poj1860 Currency Exchange(spfa判断正环)
查看>>
SQL CHECK 约束&Case when 的使用方法
查看>>
[整理]HTTPS和SSL证书
查看>>
[转载] Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
查看>>
水晶苍蝇拍:聊聊估值那些事儿——“指标”背后的故事 (2011-11-01 14:58:32)
查看>>
3.每周总结
查看>>
应用提交 App Store 上架被拒绝
查看>>
Android实现异步处理 -- HTTP请求
查看>>
数据清空js清空div里的数据问题
查看>>
Fortran中的指针使用
查看>>
移动终端app测试点总结
查看>>
14-6-27&28自学内容小结
查看>>