【黑马Android】(03)学生管理系统/动态刷新界面

学生管理系统

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima27.sutdentmanager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima27.sutdentmanager.MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dip"
        android:text="学生管理系统"
        android:textColor="#99CCFF"
        android:textSize="23sp" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:padding="5dip" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dip"
            android:paddingRight="15dip"
            android:text="姓名"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_toRightOf="@id/tv_name"
            android:paddingLeft="15dip"
            android:paddingRight="15dip"
            android:text="性别"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_toRightOf="@id/tv_sex"
            android:paddingLeft="15dip"
            android:paddingRight="15dip"
            android:text="年龄"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/tv_name"
            android:layout_alignRight="@id/tv_name"
            android:layout_below="@id/tv_name"
            android:singleLine="true" />

        <EditText
            android:id="@+id/et_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/tv_sex"
            android:layout_alignRight="@id/tv_sex"
            android:layout_below="@id/tv_sex"
            android:singleLine="true" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/tv_age"
            android:layout_alignRight="@id/tv_age"
            android:layout_below="@id/tv_age"
            android:inputType="number"
            android:singleLine="true" />

        <Button
            android:id="@+id/btn_add_student"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/et_age"
            android:layout_toRightOf="@id/et_age"
            android:text="添加学生"
            android:textSize="20sp" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/ll_student_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="1dip"
            android:orientation="vertical"
            android:padding="5dip" >
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="保存数据"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_restore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="恢复数据"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>
package com.itheima27.sutdentmanager.entities;

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 + "]";
	}
}

package com.itheima27.sutdentmanager;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

import com.itheima27.sutdentmanager.entities.Student;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private EditText etName;
	private EditText etSex;
	private EditText etAge;
	private LinearLayout llStudentList;
	private List<Student> 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<Student>();
		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();
		}
	}
}

动态刷新界面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="添加" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima28.refreshview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima28.refreshview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

package com.itheima28.refreshview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private LinearLayout llGroup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		llGroup = (LinearLayout) findViewById(R.id.ll);
	}

	public void onClick(View view) {

		// 添加一个TextView向llGroup

		// 定义一个textview对象
		TextView tv = new TextView(this);
		tv.setText("张三   女   34");

		// 把textview对象添加到linearlayout中
		llGroup.addView(tv);
	}
}
时间: 2024-08-01 11:36:22

【黑马Android】(03)学生管理系统/动态刷新界面的相关文章

Android实现学生管理系统_Android

本文实例为大家分享了Android实现学生管理系统的关键性代码,供大家参考,具体内容如下 局部效果图:   实现代码: 1.布局 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima27.sutdentmanager&

大家帮帮忙 C# windows程序设计 学生管理系统 涉及界面窗口菜单设计、数据库

问题描述 这是大一下C#的期末作业,感觉好难,不会,大神们帮帮忙.(附:本人QQ:1747960716)下面是基本要求:系统基本要求1.系统可通过菜单操作进行退出,通过左侧菜单,点击主菜单后显示子菜单2.在操作过程中要保证数据输入的准确性和可操作性,遇到非法操作应进行异常处理3.学生档案管理学生档案管理包括学生档案的建立.编辑.查询等内容.学生的档案包括学号.姓名.性别.班级.学制.专业.出生日期.家庭地址.联系电话.入学时间.考试成绩.所学课程和简单的备注说明等内容,对学生的档案可以进行录入.

新人用Vb2005建立了一个学生管理系统登录界面如下,验证密码出现问题。估计是dataset那。

问题描述 设计登录界面的时候需要账号和密码,我建立一个SQL数据库后跟VB的登录form链接成功,查询和链接都是用的sql控件,sqlconnection,sqldataAdapter,dataset,里面的查询语句写得是:SELECTzh,mmFROMmimaWHERE(zh='&"me.TextBox1.Text"&')AND(mm='&"me.TextBox2.Text"&')期中zh,mm为mima表里的字段.当我用SqlDa

简单实现Android学生管理系统(附源码)_Android

本文实例讲述了Android实现学生管理系统,分享给大家供大家参考.具体如下: (1)管理系统实现的功能主要是:学生.教师的注册登录,和选课,以及修改学生的成绩等基本简单的功能,最主要的是实现一些Dialog的使用. 界面如下: (2)主要代码如下:(个人留作笔记,如需要完整代码,在最下边免费下载) 下边是一个适配器,适配器是为了一个listvie进行设置值,其中加载的是一个itemview,适配器中还是用了继承的方法,用于通知适配器进行更新. public class CourseAdapte

05_学生管理系统,xml读写,布局的综合应用

 最终要做的项目目标:   2.编写Android清单文件AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.itheima27.sutdentmanager"    

Android For JNI(四)——C的数组,指针长度,堆内存和栈内存,malloc,学生管理系统

Android For JNI(四)--C的数组,指针长度,堆内存和栈内存,malloc,学生管理系统 好几天每写JNI了,现在任务也越来越重了,工作的强度有点高,还有好几个系列的博客要等着更新,几本书还嗷嗷待哺的等着我去看,github上的两个散漫的开源,基础入门的视频也在录制,还要学习新的知识, 都是一种挑战,不知道为何,最近懒散了,看来还得再加把劲,今天我们继续延伸一下C的一些小知识 一.数组 C的数组和JAVA也是类似的,我们写一段小程序 #include <stdio.h> #inc

代码-学生管理系统C#窗体界面

问题描述 学生管理系统C#窗体界面 我们现在需要用C#作一个管理系统,其中会很多"botten"来实现界面的跳转,求代码. 解决方案 Form2 form2=new Form; form2. Show();this.Hide; 应该是

vc++-急求用VC++2005文件写的学生管理系统,不用MFC界面,DOS就可以

问题描述 急求用VC++2005文件写的学生管理系统,不用MFC界面,DOS就可以 大致三个模块,模块功能如下 第一块:登录模块及权限设置模块 (1)管理员/学生登录模块:设置/修改密码:不同类型人员的权限设置(管理员可以修改信息,学生只能查询信息) 第二块:管理员模块 (2)面向管理员的学生信息管理模块:加入学生信息:根据各种特征方便的查找学生信息:学生信息的管理: (3)面向管理员的课程及成绩管理模块:各学年所修课程及其成绩管理. 第三块:学生模块 (4)面向学生的学生信息查询模块:根据各种

代码-急求C++2005 写的学生管理系统,不用MFC界面,求大神

问题描述 急求C++2005 写的学生管理系统,不用MFC界面,求大神 C++2005 写的学生管理系统,不用MFC界面,要有管理员和学生两种身份登陆 解决方案 #include #include #include using namespace std; typedef struct stu//定义一个结构体作为类的私有成员 { int num; string name; }; class student //学生类为基类 { protected: stu a[100]; int i, n;