Android 课程设计

时间:2022-01-01 06:57:15

 

 

基于Android平台的学生信息管理系统 1

一、系统需求分析 1

二、 系统设计 2

2.1系统模块设计 2

2.2 系统总体设计 2

2.3 系统数据库设计 2

(学生信息表) 2

课程信息表 2

成绩信息表 3

三、 系统详细设计与实现 3

3.1 主界面 3

3.2学生信息界面 5

3.3 课程信息界面 10

3.4 成绩信息界面 14

四、 系统测试 17

4.1 测试 17

4.2 测试结果 17

五、 体验心得 18

附件 Java主要源代码 19

 

 

 

基于Android平台的学生信息管理系统

 

一、系统需求分析

   随着学校的规模不断扩大,学生数量急剧增加,有关学生的各种信息量也成倍增长。面对庞大的信息量需要有学生管理系统来提高学生管理工作的效率。通过这样的系统可以做到信息的规范管理、科学统计和快速查询、修改、增加、删除等,从而减少管理方面的工作量。

本系统主要用于学校学生信息管理,总体任务是实现学生信息关系的系统化、规范化和自动化,其主要任务是用计算机对学生各种信息进行日常管理,如查询、修改 、增加、删除。

开发学生信息管理系统手机客户端旨在方便管理学生信息,使同学android智能手机就可以管理学生信息,真正做到互联网的全方位覆盖。管理学生信息系统适用于具有android智能手机的用户,在使用本软件后,学生信息管理将更加方便快捷。

1.2用例分析

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.3数据逻辑模型

 

 

 

 

 

 

 

二、系统设计

2.1系统模块设计

根据对系统的需求分析,本系统将分为3个模块:

学生信息

管理学生的基本信息,包括个人信息的添加、修改、删除。

课程信息

管理课程的基本信息,包括课程信息的添加、修改和删除。

成绩信息

管理学生的选课的成绩信息,包括成绩的登记与修改。

 

2.2 系统总体设计

   学生信息管理系统E-R图  

 

 

 

 

 

 

 

 

 

 

 

 

 

2.3 系统数据库设计

(学生信息表)

字段名

数据类型

长度

主键

索引

外键

可空

说明

sncm

int

9

 

学 号

sname

Varchar

20

 

学生姓名

ssex

Vachar

10

 

性 别

sage

varchar

10

 

年 龄

sphone

Varchar

11

 

手机号

 

(课程信息表)

字段名

数据类型

长度

主键

索引

外键

可空

说明

cnum

varchar(10)

10

 

课程编码

cname

Varchar

20

 

课程名称

credit

Smallint

10

 

学  分

 

 

(成绩信息表)

字段名

数据类型

长度

主键

索引

外键

可空

说明

sncm

int

9

 

学 号

cnum

varchar(10)

10

 

课程编码

score

int

10

 

分 数

三、系统详细设计与实现

3.1 主界面

运行程序后会进入主界面,主界面可以选择进入学生信息模块、课程信息模块以及成绩信息模块(直接点击进行跳转),方便操作,程序的主要功能一目了然。

 

 

 

 

主要代码

    <Button

        android:id="@ id/students"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="115dp"

        android:text="学  生  信  息" />

    <TextView

        android:id="@ id/home_xinxi"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="40dp"

        android:text="学生成绩管理系统"

        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button

        android:id="@ id/cs"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@ id/courses"

        android:layout_below="@ id/courses"

        android:layout_marginTop="36dp"

        android:text="成  绩  信  息" />

    <Button

        android:id="@ id/courses"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@ id/students"

        android:layout_below="@ id/students"

        android:layout_marginTop="30dp"

        android:text="课  程  信  息" />

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.student_layout);

setTitle("学生信息管理");

search = (Button) this.findViewById(R.id.search_student_button);

search_text = (EditText) this.findViewById(R.id.to_search_student);

listView = (ListView) findViewById(R.id.student_listview);

add_student = (Button) this.findViewById(R.id.add_student);

delete_student = (Button) this.findViewById(R.id.delete_student);

db = new MyDB(StudentsActivity.this);

updatelistview();

listView.setOnItemClickListener(new OnItemClickListener() {

 

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

ListView myListView=(ListView)parent;  

   HashMap<String, Object> item=(HashMap<String, Object>)myListView.getItemAtPosition(position);  

   popview(item,"modify");//添加弹出窗口

   

}

 

});

search.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

updatelistview();

}

});

3.2学生信息界面

学生信息界面主要显示学生的信息,可以在此界面对学生的信息进行查询、添加、删除和修改。

添加时直接点击添加,会弹出添加界面,进行添加即可

 

 

 

查询时搜索学号即可查询信息

 

删除时输入学号进行删除

   

 

修改时点击信息会弹出界面,直接进行修改即可

 

主要代码

 

 <LinearLayout

        android:id="@ id/nono"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <TextView

            android:id="@ id/lll"

            android:layout_width="100dp"

            android:layout_height="40dp"

            android:text="搜 索 学 号:" />

 

        <EditText

            android:id="@ id/to_search_student"

            android:layout_width="160dp"

            android:layout_height="40dp"

            android:ems="10"

            android:inputType="number" />

 

        <Button

            android:id="@ id/search_student_button"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="查询" />

    </LinearLayout>

<LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@ id/delete_student"

            android:layout_width="160dp"

            android:layout_height="wrap_content"

            android:layout_weight="0.79"

            android:text="删  除" />

 

        <Button

            android:id="@ id/add_student"

            android:layout_width="160dp"

            android:layout_height="wrap_content"

            android:layout_weight="0.79"

            android:text="添  加" />

 

    </LinearLayout>

    <LinearLayout

        android:id="@ id/stu_head"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <TextView

            android:id="@ id/student_view_snum"

            android:layout_width="80dp"

            android:layout_height="30dp"

            android:text="学 号" />

 

        <TextView

            android:id="@ id/student_view_sname"

            android:layout_width="50dp"

            android:layout_height="30dp"

            android:text="姓名"/>

 

        <TextView

            android:id="@ id/student_view_ssex"

            android:layout_width="30dp"

            android:layout_height="30dp"

            android:text="性别" />

 

        <TextView

            android:id="@ id/student_view_sage"

            android:layout_width="50dp"

            android:layout_height="30dp"

            android:text="年 龄" />

 

        <TextView

            android:id="@ id/student_view_sphone"

            android:layout_width="100dp"

            android:layout_height="30dp"

            android:text="电   话" />

    </LinearLayout>

 

public void AddStudent(Student student) {

String sql ="insert into Students (Snum, Sname, Ssex, Sage, Sphone) values (?, ?, ?, ?, ?)";

Object[] bindArgs = { student.getSnum(), student.getSname(), student.getSsex(), student.getSage(), student.getSphone()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

 

public void delStudents(String snumber){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Students where Snum = ?";//删students表中的某个学生

Object[] bindArgs = {snumber};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS where Snum = ?";//删CS成绩表中该学生的全部成绩

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

public void delALLStudents(){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Students ";//删students表中的某个学生

Object[] bindArgs = {};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS ";//删CS成绩表中该学生的全部成绩

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

 

public void updateStudent(Student student){

String sql = "update Students set  Sname = ?, Ssex = ?, Sage = ?, Sphone = ? where Snum = ?";

Object[] bindArgs = {student.getSname(),

student.getSsex(),student.getSage(),student.getSphone(), student.getSnum()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

public List<Student> findALLStudent(){

String sql = "select * from Students order by Snum asc";

String[] selectionArgs = {};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

List<Student> students =new ArrayList<Student>();

while(cursor.moveToNext()){

Student student = new Student(cursor.getString(0), cursor.getString(1),

cursor.getString(2), cursor.getInt(3), cursor.getString(4));

students.add(student);

}

return students;

}

public Student findStudent(String snumber){

String sql = "select * from Students where Snum = ?";

String[] selectionArgs = {snumber};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

if(cursor.moveToFirst()){

return new Student(cursor.getString(0), cursor.getString(1),

cursor.getString(2), cursor.getInt(3), cursor.getString(4));

}

return null;

 

3.3 课程信息界面

  课程信息界面可以查看课程信息,进行课程编码、课程名称、课程学分的增删改查。

 

添加课程信息:过程和学生信息的一样

 

 

 

删除课程信息

 

修改课程信息

 

 

 

主要代码

  <LinearLayout

        android:id="@ id/nono"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <TextView

            android:id="@ id/ccc"

            android:layout_width="100dp"

            android:layout_height="40dp"

            android:text="搜索课程编码:" />

 

        <EditText

            android:id="@ id/to_search_Course"

            android:layout_width="160dp"

            android:layout_height="40dp"

            android:ems="10"/>

 

        <Button

            android:id="@ id/search_course_button"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="查询" />

    </LinearLayout>

      <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@ id/delete_course"

            android:layout_width="160dp"

            android:layout_height="wrap_content"

            android:layout_weight="0.79"

            android:text="删  除" />

 

        <Button

            android:id="@ id/add_course"

            android:layout_width="160dp"

            android:layout_height="wrap_content"

            android:layout_weight="0.79"

            android:text="添  加" />

 

    </LinearLayout>

       <LinearLayout

        android:id="@ id/stu_head"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <TextView

            android:id="@ id/course_view_cnum"

            android:layout_width="108dp"

            android:layout_height="30dp"

            android:text="课程编码" />

 

        <TextView

            android:id="@ id/course_view_cname"

            android:layout_width="112dp"

            android:layout_height="30dp"

            android:text="课程名称" />

 

<TextView

    android:id="@ id/course_view_credit"

    android:layout_width="56dp"

    android:layout_height="30dp"

    android:text="学  分" />

 

    </LinearLayout>

public void AddCourse(Course course) {

String sql ="insert into Courses (Cnum, Cname, Ccredit) values (?, ?, ?)";

Object[] bindArgs = {course.getCnum(),course.getCname(),course.getCcredit()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

public void delCourse(String cnumber){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Courses where Cnum = ?";

Object[] bindArgs = {cnumber};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS where Cnum = ?";

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

public void delALLCourse(){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Courses";

Object[] bindArgs = {};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS";

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

 

public void updateCourse(Course course){

String sql = "update Courses set Cname = ?, Ccredit = ? where Cnum = ? ";

Object[] bindArgs = {course.getCname(), course.getCcredit(), course.getCnum()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

3.4 成绩信息界面

   成绩界面可以查看学生的成绩,学生的成绩以学号+课程编码+分数的形式显示。同样的,在这个界面同样可以对学生的成绩信息进行增删改查。

 

增加学生成绩信息;

 

删除学生成绩信息;

 

 

修改学生成绩信息;

 

主要代码

<LinearLayout

        android:id="@ id/llllbn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <TextView

            android:id="@ id/bfgbfd"

            android:layout_width="100dp"

            android:layout_height="40dp"

            android:text="学号 课程编码:" />

        <EditText

            android:id="@ id/to_search_CS_S"

            android:layout_width="80dp"

            android:layout_height="40dp"

            android:ems="5"

            android:inputType="number" />

        <EditText

            android:id="@ id/to_search_CS_C"

            android:layout_width="80dp"

            android:layout_height="40dp"

            android:ems="5"/>

        <Button

            android:id="@ id/search_cs_button"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="查询" />

    </LinearLayout>

    

        <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@ id/delete_cs"

            android:layout_width="160dp"

            android:layout_height="wrap_content"

            android:layout_weight="0.79"

            android:text="删  除" />

 

        <Button

            android:id="@ id/add_cs"

            android:layout_width="160dp"

            android:layout_height="wrap_content"

            android:layout_weight="0.79"

            android:text="添  加" />

 

    </LinearLayout>

    

       <LinearLayout

        android:id="@ id/cs_head"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <TextView

            android:id="@ id/cs_view_snum"

            android:layout_width="108dp"

            android:layout_height="30dp"

            android:text="学号" />

 

        <TextView

            android:id="@ id/cs_view_cnum"

            android:layout_width="112dp"

            android:layout_height="30dp"

            android:text="课程编码" />

 

<TextView

    android:id="@ id/cs_view_score"

    android:layout_width="56dp"

    android:layout_height="30dp"

    android:text="分数" />

public void AddCS(CS cs) throws Exception {

if(findStudent(cs.getSnum()) != null && findCourse(cs.getCnum()) != null){

String sql ="insert into CS (Snum, Cnum, Score) values (?, ?, ?)";

Object[] bindArgs = {cs.getSnum(),cs.getCnum(),cs.getScore()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

else{

throw new Exception("can‘t add this score");

}

 

}

public void delCS(String snumber,String cnumber){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from CS where Snum = ? and Cnum = ?";

Object[] bindArgs = {snumber,cnumber};

db.execSQL(sql, bindArgs);

}

public void delALLCS(){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from CS";

Object[] bindArgs = {};

db.execSQL(sql, bindArgs);

}

public void updateCS(CS cs){

String sql = "update CS set  Score = ? where Snum = ? and Cnum = ?";

Object[] bindArgs = {cs.getScore(), cs.getSnum(),cs.getCnum()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

四、系统测试

4.1 测试

1.测试系统能否正常运行

2.测试数据库能否顺利连接

3.测试系统各个页面能否正常跳转

4.测试增删改查功能能否正常使用

5.测试学生信息是否正常显示

4.2 测试结果

    系统能够正常运行;数据库顺利连接;页面跳转正常;增删改查能够正常使用;学生信息显示正常。

 

 

五、体验心得

通过这次的程序设计,我学到了很多,首先就是能够更加熟练的使用和深入的了解Java语言这门通用的计算机语言,还有就是可以自己编写程序了,还是很有成就感的。还有就是光靠书本的知识的确不行,还是要理论联系实践才行。因此不断的练习是必要的,上机实践更重要。通过这次的程序编写让我对编程产生了更大的兴趣,我相信以后我们都会越来越好的。

我从一开始的懵懂到现在的有点理解可以说和老师的教导分不开的。虽然俗语说师傅领进门,修行靠个人,所以师傅的作用还是不可取代的,以前上课时老师教的代码给了我很大的帮助,非常感谢老师!

 

附件 Java主要源代码

 StudentActivity.java(对学生信息界面的操作):

public class StudentsActivity extends Activity{

private Button search = null;

private EditText search_text = null;

private ListView listView = null;

private MyDB db = null;

private Button add_student = null;

private Button delete_student = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.student_layout);

setTitle("学生信息管理");

search = (Button) this.findViewById(R.id.search_student_button);

search_text = (EditText) this.findViewById(R.id.to_search_student);

listView = (ListView) findViewById(R.id.student_listview);

add_student = (Button) this.findViewById(R.id.add_student);

delete_student = (Button) this.findViewById(R.id.delete_student);

db = new MyDB(StudentsActivity.this);

updatelistview();

listView.setOnItemClickListener(new OnItemClickListener() {

 

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

ListView myListView=(ListView)parent;  

   HashMap<String, Object> item=(HashMap<String, Object>)myListView.getItemAtPosition(position);  

   popview(item,"modify");//添加弹出窗口

   

}

 

});

search.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

updatelistview();

}

});

add_student.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

HashMap<String, Object> item = null;

popview(item, "add");

}

});

delete_student.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

try {

if(search_text.getText().toString().equals("0000")){

db.delALLStudents();

}

else{

db.delStudents(search_text.getText().toString());

}

search_text.setText("");

updatelistview();

} catch (Exception e) {

// TODO: handle exception

Toast.makeText(StudentsActivity.this, "delete student error!" , Toast.LENGTH_SHORT).show();

}

 

}

});

 

}

private void popview(HashMap<String, Object> item,final String mode) {

   // 取得自定义View

   try {

   LayoutInflater layoutInflater = LayoutInflater.from(StudentsActivity.this);

   View myLoginView = layoutInflater.inflate(R.layout.student_edit, null);

   final EditText modify_snum =  (EditText) myLoginView.findViewById(R.id.modify_snum_txt);

   final EditText modify_sname = (EditText) myLoginView.findViewById(R.id.modify_sname_txt);    

   final EditText modify_sage = (EditText) myLoginView.findViewById(R.id.modify_sage_txt);    

   final EditText modify_sphone = (EditText) myLoginView.findViewById(R.id.modify_sphone_txt);    

   final RadioButton maleButton = (RadioButton) myLoginView.findViewById(R.id.radio_button_male);

   final RadioButton femaleButton = (RadioButton) myLoginView.findViewById(R.id.radio_button_female);

   modify_snum.setText("");

   modify_sname.setText("");

   modify_sage.setText("0");

   modify_sphone.setText("");

   modify_snum.setEnabled(true);

   if(mode.equals("modify")){

   modify_snum.setText(item.get("Snum").toString());

   modify_snum.setEnabled(false);

   modify_sname.setText(item.get("Sname").toString());

   modify_sage.setText(item.get("Sage").toString());

   modify_sphone.setText(item.get("Sphone").toString());

   if(item.get("Ssex").toString().equals("男")){

   maleButton.setChecked(true);

   }

   else{

   femaleButton.setChecked(true);

   }

   }

  

   Dialog alertDialog = new AlertDialog.Builder(StudentsActivity.this).

    setView(myLoginView).

    setPositiveButton("确认", new DialogInterface.OnClickListener() {

 

     public void onClick(DialogInterface dialog, int which) {

      // TODO Auto-generated method stub

      

try {

 

      String snum =  modify_snum.getText().toString() ;

 String sname = modify_sname.getText().toString();

String sphone = modify_sphone.getText().toString();

int sage = Integer.valueOf(modify_sage.getText().toString());

String ssex = "女";

if(maleButton.isChecked()){

ssex = "男";

}

if(snum.equals("")){

 Toast.makeText(StudentsActivity.this, "can‘t add a student without Snum!", Toast.LENGTH_SHORT).show();

return;

}

Student student = new Student(snum, sname , ssex , sage, sphone);

if(mode.equals("modify")){

db.updateStudent(student);

updatelistview();

}

else{

try {

db.AddStudent(student);

updatelistview();

} catch (Exception e) {

// TODO: handle exception

 Toast.makeText(StudentsActivity.this, "add student error", Toast.LENGTH_SHORT).show();

}

}

      

} catch (Exception e) {

// TODO: handle exception

   Toast.makeText(StudentsActivity.this, "update student error", Toast.LENGTH_SHORT).show();    

 

}

 

     }

    }).

    setNegativeButton("撤销", new DialogInterface.OnClickListener() {

 

     public void onClick(DialogInterface dialog, int which) {

      // TODO Auto-generated method stub

      Toast.makeText(StudentsActivity.this, "", Toast.LENGTH_SHORT).show();

     }

    }).

    create();

   

    alertDialog.show();

   } catch (Exception e) {

// TODO: handle exception

   Toast.makeText(StudentsActivity.this, "runing wrong!", Toast.LENGTH_SHORT).show();    

}

}

private void updatelistview() {

try {

String string = search_text.getText().toString();

List<Student> students = new ArrayList<Student>();

List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();

if(!string.equals("")){

Student student = db.findStudent(string);

students.add(student);

}

else {

students = db.findALLStudent();

}

 

Iterator<Student> it = students.iterator();

while(it.hasNext()){

HashMap<String, Object> hashMap=new HashMap<String, Object>();

Student student =  it.next();

hashMap.put("Snum",student.getSnum() );  

hashMap.put("Sname",student.getSname() );

hashMap.put("Ssex",student.getSsex() );

hashMap.put("Sage",student.getSage() );

hashMap.put("Sphone",student.getSphone() );

data.add(hashMap);

}

 

SimpleAdapter adapter=

new SimpleAdapter(StudentsActivity.this, data, R.layout.student_view,

new String[]{"Snum","Sname","Ssex","Sage","Sphone"},

new int[]{R.id.student_view_snum,R.id.student_view_sname,R.id.student_view_ssex,R.id.student_view_sage,R.id.student_view_sphone});

listView.setAdapter(adapter);

 

} catch (Exception e) {

// TODO: handle exception

   Toast.makeText(StudentsActivity.this, "can‘t find this student!" , Toast.LENGTH_SHORT).show();

}

 

}

 

}

 

CourseActivity.java(对学生信息界面的操作):

public class CourseActivity extends Activity {

private Button search = null;

private EditText search_text = null;

private ListView listView = null;

private MyDB db = null;

private Button add_course = null;

private Button delete_course = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.course_layout);

setTitle("课程信息管理");

search = (Button) this.findViewById(R.id.search_course_button);

search_text = (EditText) this.findViewById(R.id.to_search_Course);

listView = (ListView) findViewById(R.id.course_listview);

add_course = (Button) this.findViewById(R.id.add_course);

delete_course = (Button) this.findViewById(R.id.delete_course);

db = new MyDB(CourseActivity.this);

updatelistview();

listView.setOnItemClickListener(new OnItemClickListener() {

 

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

ListView myListView=(ListView)parent;  

   HashMap<String, Object> item=(HashMap<String, Object>)myListView.getItemAtPosition(position);

   popview(item,"modify");//添加弹出窗口

   

}

 

});

search.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

updatelistview();

}

});

add_course.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

HashMap<String, Object> item = null;

popview(item, "add");

}

});

delete_course.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

try {

if(search_text.getText().toString().equals("0000")){

db.delALLCourse();

}

else{

db.delCourse(search_text.getText().toString());

}

search_text.setText("");

updatelistview();

} catch (Exception e) {

// TODO: handle exception

Toast.makeText(CourseActivity.this, "delete course error!" , Toast.LENGTH_SHORT).show();

}

 

}

});

 

}

private void popview(HashMap<String, Object> item,final String mode) {

   // 取得自定义View

   try {

   LayoutInflater layoutInflater = LayoutInflater.from(CourseActivity.this);

   View myLoginView = layoutInflater.inflate(R.layout.course_edit, null);

   final EditText modify_cnum =  (EditText) myLoginView.findViewById(R.id.modify_cnum_txt);

   final EditText modify_cname = (EditText) myLoginView.findViewById(R.id.modify_cname_txt);    

   final EditText modify_credit = (EditText) myLoginView.findViewById(R.id.modify_credit_txt);  

   modify_cnum.setText("");

   modify_cname.setText("");

   modify_credit.setText("0");

   modify_cnum.setEnabled(true);

   if(mode.equals("modify")){

   modify_cnum.setText(item.get("Cnum").toString());

   modify_cnum.setEnabled(false);

   modify_cname.setText(item.get("Cname").toString());

   modify_credit.setText(item.get("Ccredit").toString());

   }

  

   Dialog alertDialog = new AlertDialog.Builder(CourseActivity.this).

    setView(myLoginView).

    setPositiveButton("确认", new DialogInterface.OnClickListener() {

 

     public void onClick(DialogInterface dialog, int which) {

      // TODO Auto-generated method stub

      

try {

 

      String cnum =  modify_cnum.getText().toString() ;

 String cname = modify_cname.getText().toString();

float credit = Float.valueOf(modify_credit.getText().toString());

if(cnum.equals("")){

 Toast.makeText(CourseActivity.this, "can‘t add a course without Cnum!", Toast.LENGTH_SHORT).show();

return;

}

Course course = new Course(cnum, cname, credit);

if(mode.equals("modify")){

db.updateCourse(course);

updatelistview();

}

else{

try {

db.AddCourse(course);

updatelistview();

} catch (Exception e) {

// TODO: handle exception

 Toast.makeText(CourseActivity.this, "add course error", Toast.LENGTH_SHORT).show();

}

}

      

} catch (Exception e) {

// TODO: handle exception

   Toast.makeText(CourseActivity.this, "update course error", Toast.LENGTH_SHORT).show();    

 

}

 

     }

    }).

    setNegativeButton("撤销", new DialogInterface.OnClickListener() {

 

     public void onClick(DialogInterface dialog, int which) {

      // TODO Auto-generated method stub

      Toast.makeText(CourseActivity.this, "", Toast.LENGTH_SHORT).show();

     }

    }).

    create();    

    alertDialog.show();

   } catch (Exception e) {

// TODO: handle exception

   Toast.makeText(CourseActivity.this, "runing wrong!", Toast.LENGTH_SHORT).show();    

}

}

private void updatelistview() {

try {

String string = search_text.getText().toString();

List<Course> courses = new ArrayList<Course>();

List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();

if(!string.equals("")){

Course course = db.findCourse(string);

courses.add(course);

}

else {

courses = db.findALLCourse();

}

 

Iterator<Course> it = courses.iterator();

while(it.hasNext()){

HashMap<String, Object> hashMap=new HashMap<String, Object>();

Course course =  it.next();

hashMap.put("Cnum",course.getCnum());  

hashMap.put("Cname",course.getCname());

hashMap.put("Ccredit",course.getCcredit());

data.add(hashMap);

}

 

SimpleAdapter adapter=

new SimpleAdapter(CourseActivity.this, data, R.layout.course_view,

new String[]{"Cnum","Cname","Ccredit"},

new int[]{R.id.course_view_cnum,R.id.course_view_cname,R.id.course_view_credit});

listView.setAdapter(adapter);

 

} catch (Exception e) {

// TODO: handle exception

   Toast.makeText(CourseActivity.this, "can‘t find this course!" , Toast.LENGTH_SHORT).show();

}

 

}

 

}

 

CSActivity.java(对成绩信息界面的操作):

public class CSActivity extends Activity {

private Button search = null;

private EditText search_s = null;

private EditText search_c = null;

private ListView listView = null;

private MyDB db = null;

private Button add_cs = null;

private Button delete_cs = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.cs_layout);

setTitle("成绩信息管理");

search = (Button) this.findViewById(R.id.search_cs_button);

search_s = (EditText) this.findViewById(R.id.to_search_CS_S);

search_c = (EditText) this.findViewById(R.id.to_search_CS_C);

listView = (ListView) findViewById(R.id.cs_listview);

add_cs = (Button) this.findViewById(R.id.add_cs);

delete_cs = (Button) this.findViewById(R.id.delete_cs);

db = new MyDB(CSActivity.this);

updatelistview();

listView.setOnItemClickListener(new OnItemClickListener() {

 

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

ListView myListView=(ListView)parent;  

   HashMap<String, Object> item=(HashMap<String, Object>)myListView.getItemAtPosition(position);  

   popview(item,"modify");//添加弹出窗口

   

}

 

});

search.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

updatelistview();

}

});

add_cs.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

HashMap<String, Object> item = null;

popview(item, "add");

}

});

delete_cs.setOnClickListener(new OnClickListener() {

 

public void onClick(View v) {

// TODO Auto-generated method stub

try {

if(search_s.getText().toString().equals("0000")){

db.delALLCourse();

}

else{

db.delCS(search_s.getText().toString(), search_c.getText().toString());

//(search_text.getText().toString());

}

search_s.setText("");

search_c.setText("");

updatelistview();

} catch (Exception e) {

// TODO: handle exception

Toast.makeText(CSActivity.this, "delete score error!" , Toast.LENGTH_SHORT).show();

}

 

}

});

 

}

private void popview(HashMap<String, Object> item,final String mode) {

   // 取得自定义View

   try {

   LayoutInflater layoutInflater = LayoutInflater.from(CSActivity.this);

   View myLoginView = layoutInflater.inflate(R.layout.cs_edit, null);

   final EditText modify_snum =  (EditText) myLoginView.findViewById(R.id.modify_snum_txt);

   final EditText modify_cnum = (EditText) myLoginView.findViewById(R.id.modify_cnum_txt);    

   final EditText modify_score = (EditText) myLoginView.findViewById(R.id.modify_score_txt);

   modify_snum.setText("");

   modify_cnum.setText("");

   modify_score.setText("0");

   modify_snum.setEnabled(true);

   modify_cnum.setEnabled(true);

   if(mode.equals("modify")){

   modify_snum.setText(item.get("Snum").toString());

   modify_snum.setEnabled(false);

   modify_cnum.setEnabled(false);

   modify_cnum.setText(item.get("Cnum").toString());

   modify_score.setText(item.get("Score").toString());

   }

  

   Dialog alertDialog = new AlertDialog.Builder(CSActivity.this).

    setView(myLoginView).

    setPositiveButton("确认", new DialogInterface.OnClickListener() {

 

     public void onClick(DialogInterface dialog, int which) {

      // TODO Auto-generated method stub       

try {

 

      String snum =  modify_snum.getText().toString() ;

 String cnum = modify_cnum.getText().toString();

float score = Float.valueOf(modify_score.getText().toString());

if(snum.equals("") || cnum.equals("")){

 Toast.makeText(CSActivity.this, "can‘t add a score without Snum & Cnum!", Toast.LENGTH_SHORT).show();

return;

}

// Student student = new Student(snum, sname , ssex , sage, sphone);

CS cs = new CS(snum, cnum, score);

if(mode.equals("modify")){

db.updateCS(cs);

updatelistview();

}

else{

try {

db.AddCS(cs);

updatelistview();

} catch (Exception e) {

// TODO: handle exception

 Toast.makeText(CSActivity.this, "add score error", Toast.LENGTH_SHORT).show();

}

}

      

} catch (Exception e) {

// TODO: handle exception

   Toast.makeText(CSActivity.this, "update score error", Toast.LENGTH_SHORT).show();    

 

}

 

     }

    }).

    setNegativeButton("撤销", new DialogInterface.OnClickListener() {

 

     public void onClick(DialogInterface dialog, int which) {

      // TODO Auto-generated method stub

      Toast.makeText(CSActivity.this, "", Toast.LENGTH_SHORT).show();

     }

    }).

    create();

   

    alertDialog.show();

   } catch (Exception e) {

// TODO: handle exception

   Toast.makeText(CSActivity.this, "runing wrong!", Toast.LENGTH_SHORT).show();    

}

}

private void updatelistview() {

try {

String s = search_s.getText().toString();

String c = search_c.getText().toString();

List<CS> Css = new ArrayList<CS>();

 

List<HashMap<String, Object>> data1 = new ArrayList<HashMap<String,Object>>();

if(s.equals("") && c.equals("")){

Css = db.findALLCS();

}

else if(s.equals("") && (!c.equals(""))){

Css = db.findCSbyCnum(c);

}

else if((!s.equals("")) && c.equals("")){

Css = db.findCSbySnum(s);

}else{

CS cs = db.findCS(c, s);

Css.add(cs);

}

Iterator<CS> it = Css.iterator();

int i = 0;

while(it.hasNext()){

HashMap<String, Object> hashMap=new HashMap<String, Object>();

CS cs =  it.next();

hashMap.put("Snum",cs.getSnum() );  

hashMap.put("Cnum",cs.getCnum() );

hashMap.put("Score",cs.getScore() );

data1.add(hashMap);

 i = i 1;

}

 Toast.makeText(CSActivity.this, "一共:" i , Toast.LENGTH_SHORT).show();

 

 

SimpleAdapter adapter=

new SimpleAdapter(CSActivity.this, data1, R.layout.cs_view,

new String[]{"Snum","Cnum","Score"},

new int[]{R.id.cs_view_snum,R.id.cs_view_cnum,R.id.cs_view_score});

listView.setAdapter(adapter);

 

} catch (Exception e) {

// TODO: handle exception

   Toast.makeText(CSActivity.this, "can‘t find this score!" , Toast.LENGTH_SHORT).show();

}

 

}

 

}

 

MyDBhelper.java(创建数据库):

public class MyDBhelper extends SQLiteOpenHelper{

private static final String name="contants";//数据库名称

private static final int version= 1;//数据库版本

public MyDBhelper(Context context) {

super(context, name, null, version);

// TODO Auto-generated constructor stub

}

 

@Override

public void onCreate(SQLiteDatabase db) {

// TODO Auto-generated method stub

String students = "create table if not exists Students (Snum varchar(10) primary key, Sname varchar(6), Ssex varchar(2), Sage integer(3), Sphone varchar(12))";

String courses = "create table if not exists Courses (Cnum varchar(4) primary key, Cname varchar(10), Ccredit real(3))";

String cs = "create table if not exists CS (Snum varchar(10) , Cnum varchar(4), Score real(3), primary key (Snum,Cnum)) ";

Object[] bindArgs = {};

db.execSQL(students, bindArgs);

db.execSQL(courses, bindArgs);

db.execSQL(cs, bindArgs);

}

 

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// TODO Auto-generated method stub

db.execSQL("DROP TABLE IF EXISTS Students");

db.execSQL("DROP TABLE IF EXISTS Courses");

db.execSQL("DROP TABLE IF EXISTS CS");

this.onCreate(db);

}

 

 

}

MyDB.java(对数据库进行增删改查):

 

public class MyDB{

private MyDBhelper dBhelper;

private SQLiteDatabase mydb;

public MyDB(Context context) {

// TODO Auto-generated constructor stub

this.dBhelper = new MyDBhelper(context);

}

/*增*/

public void AddStudent(Student student) {

String sql ="insert into Students (Snum, Sname, Ssex, Sage, Sphone) values (?, ?, ?, ?, ?)";

Object[] bindArgs = { student.getSnum(), student.getSname(), student.getSsex(), student.getSage(), student.getSphone()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

public void AddCourse(Course course) {

String sql ="insert into Courses (Cnum, Cname, Ccredit) values (?, ?, ?)";

Object[] bindArgs = {course.getCnum(),course.getCname(),course.getCcredit()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

public void AddCS(CS cs) throws Exception {

if(findStudent(cs.getSnum()) != null && findCourse(cs.getCnum()) != null){

String sql ="insert into CS (Snum, Cnum, Score) values (?, ?, ?)";

Object[] bindArgs = {cs.getSnum(),cs.getCnum(),cs.getScore()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

else{

throw new Exception("can‘t add this score");

}

 

}

/*删*/

public void delStudents(String snumber){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Students where Snum = ?";//删students表中的某个学生

Object[] bindArgs = {snumber};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS where Snum = ?";//删CS成绩表中该学生的全部成绩

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

public void delALLStudents(){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Students ";//删students表中的某个学生

Object[] bindArgs = {};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS ";//删CS成绩表中该学生的全部成绩

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

public void delCourse(String cnumber){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Courses where Cnum = ?";

Object[] bindArgs = {cnumber};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS where Cnum = ?";

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

public void delALLCourse(){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from Courses";

Object[] bindArgs = {};

db.execSQL(sql, bindArgs);

try {

sql = "delete from CS";

db.execSQL(sql, bindArgs);

} catch (Exception e) {

// TODO: handle exception

}

 

}

public void delCS(String snumber,String cnumber){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from CS where Snum = ? and Cnum = ?";

Object[] bindArgs = {snumber,cnumber};

db.execSQL(sql, bindArgs);

}

public void delALLCS(){

        SQLiteDatabase db = this.dBhelper.getWritableDatabase();

        String sql = "delete from CS";

Object[] bindArgs = {};

db.execSQL(sql, bindArgs);

}

/*改*/

public void updateStudent(Student student){

String sql = "update Students set  Sname = ?, Ssex = ?, Sage = ?, Sphone = ? where Snum = ?";

Object[] bindArgs = {student.getSname(),

student.getSsex(),student.getSage(),student.getSphone(), student.getSnum()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

public void updateCourse(Course course){

String sql = "update Courses set Cname = ?, Ccredit = ? where Cnum = ? ";

Object[] bindArgs = {course.getCname(), course.getCcredit(), course.getCnum()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

public void updateCS(CS cs){

String sql = "update CS set  Score = ? where Snum = ? and Cnum = ?";

Object[] bindArgs = {cs.getScore(), cs.getSnum(),cs.getCnum()};

mydb = this.dBhelper.getWritableDatabase();

mydb.execSQL(sql, bindArgs);

}

/*查*/

public Student findStudent(String snumber){

String sql = "select * from Students where Snum = ?";

String[] selectionArgs = {snumber};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

if(cursor.moveToFirst()){

return new Student(cursor.getString(0), cursor.getString(1),

cursor.getString(2), cursor.getInt(3), cursor.getString(4));

}

return null;

}

public Course findCourse(String cnumber){

String sql = "select * from Courses where Cnum = ?";

String[] selectionArgs = {cnumber};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

if(cursor.moveToFirst()){

return new Course(cursor.getString(0),cursor.getString(1), cursor.getFloat(2));

}

return null;

}

public CS findCS(String cnumber,String snumber){

String sql = "select * from CS where Snum = ? and Cnum = ?";

String[] selectionArgs = {snumber ,cnumber};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

if(cursor.moveToFirst()){

return new CS(cursor.getString(0), cursor.getString(1), cursor.getFloat(2));

}

return null;

}

public List<CS> findCSbyCnum(String cnumber){

String sql = "select * from CS where Cnum = ?";

String[] selectionArgs = {cnumber};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

List<CS> css = new ArrayList<CS>();

while(cursor.moveToNext()){

CS cs1  = new CS(cursor.getString(0),cursor.getString(1), cursor.getFloat(2));

css.add(cs1);

}

return css;

}

public List<CS> findCSbySnum(String snumber){

String sql = "select * from CS where Snum = ?";

String[] selectionArgs = {snumber};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

List<CS> css = new ArrayList<CS>();

while(cursor.moveToNext()){

CS cs1  = new CS(cursor.getString(0),cursor.getString(1), cursor.getFloat(2));

css.add(cs1);

}

return css;

}

/*findALL函数*/

public List<Student> findALLStudent(){

String sql = "select * from Students order by Snum asc";

String[] selectionArgs = {};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

List<Student> students =new ArrayList<Student>();

while(cursor.moveToNext()){

Student student = new Student(cursor.getString(0), cursor.getString(1),

cursor.getString(2), cursor.getInt(3), cursor.getString(4));

students.add(student);

}

return students;

}

public List<Course> findALLCourse(){

String sql = "select * from Courses order by Cnum asc";

String[] selectionArgs = {};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

List<Course> courses = new ArrayList<Course>();

while(cursor.moveToNext()){

Course course = new Course(cursor.getString(0),cursor.getString(1), cursor.getFloat(2));

courses.add(course);

}

return courses;

}

public List<CS> findALLCS(){

String sql = "select * from CS order by Snum asc";

String[] selectionArgs = {};

mydb = this.dBhelper.getReadableDatabase();

Cursor cursor = mydb.rawQuery(sql, selectionArgs);

List<CS> css = new ArrayList<CS>();

while(cursor.moveToNext()){

CS cs1  = new CS(cursor.getString(0),cursor.getString(1), cursor.getFloat(2));

css.add(cs1);

}

return css;