设计内容
制作一个学习助手,不仅可以帮助学生查询成绩,还能够帮老师查询班级所有同学成绩,某课 程的所有学生成绩,还能够帮学生进行查询课程的功能,既能够根据日期来查询哪一天学生的所有课程,还能够根据课程查询学生的所有课程,还可以查询第几周的课程,实现一个既能够帮助学生,又能够帮助老师的学习助手
以下图是功能实现图和界面图



设计流程图

实现方法
1.数据库和表的建立,及插入原始数据和其使用(本实验未曾使用可视化数据库)
以下是代码的实现
建立数据库及插入原始数据代码
public static class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "itcast.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table Student(_id integer primary key autoincrement,stid verchar(10),name verchar(10),classes verchar(10),english verchar(10),chinese verchar(10))");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
btn_four.setOnClickListener(new View.OnClickListener() {
private String stid;
private String name;
private String classes;
private int english;
private int chinese;
SQLiteDatabase db;
ContentValues values;
@Override
public void onClick(View view) {
db = myHelper.getReadableDatabase();
db.execSQL("DELETE FROM Student");
values = new ContentValues();
stid = "1";
name = "子瑜";
classes = "美术三班";
english = 86;
chinese = 90;
values.put("stid", stid);
values.put("name", name);
values.put("classes", classes);
values.put("english", english);
values.put("chinese", chinese);
db.insert("Student", null, values);
stid = "2";
name = "张伟";
classes = "语言二班";
english = 76;
chinese = 92;
values.put("stid", stid);
values.put("name", name);
values.put("classes", classes);
values.put("english", english);
values.put("chinese", chinese);
db.insert("Student", null, values);
Toast.makeText(Activity2.this, "信息已添加", Toast.LENGTH_SHORT).show();
db.close();
}
});
使用数据库进行查询代码
public void onClick(View v) {
SQLiteDatabase db ;
String tv_1;
switch (v.getId()) {
case R.id.btn_1:
tv_1=Tv_1.getText().toString();
Activity2.MyHelper myHelper=new Activity2.MyHelper(this);
db=myHelper.getReadableDatabase();
Toast.makeText(this, "建立数据库", Toast.LENGTH_SHORT).show();
//String[] column={"stid"};
Cursor cursor = db.query("Student",null , "stid=?", new String[]{tv_1+""}, null, null, null);
if (cursor.getCount() == 0) {
mTvShow.setText("");
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
} else {
cursor.moveToFirst();
mTvShow.setText("stid:" + cursor.getString(1) + "name:" + cursor.getString(2)+"classes:"+cursor.getString(3)+"english:"+cursor.getString(4)+"chinese:"+cursor.getString(5));
}
cursor.close();
db.close();
break;
}
}
2.界面布局
(1)本小节主要是完成界面布局,首先添加图片按钮实现界面的跳转的同时增加美观。
(2)再添加日历查询课程组件,只需要点击按钮就会出现日历形式的日期表,选择日期点击查询就会获得那一天的课表。
(3)再是添加下拉框选想要查询的课程,进行某课程的成绩查询
(4)添加Edittext组件获得学号进行学生的成绩查询,或者学生所在的班级所有学生成绩查询和课程所有同学成绩查询
(1)图片按钮的实现代码如下
首先将布局改成相对布局RelativeLayout,再添加ImageButton控件,对其进行位置的设置,background进行图片设置,在其下方添加TextView控件,解释其功能

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@mipmap/a1"
>
<ImageButton
android:id="@+id/btn_1"
android:layout_width="100dp"
android:layout_height="130dp"
android:background="@mipmap/a3"
android:layout_marginTop="80dp"
android:layout_marginLeft="90dp"
>
</ImageButton>
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成绩查询"
android:textColor="#AA99DA"
android:textSize="25sp"
android:layout_below="@+id/btn_1"
android:layout_alignLeft="@+id/btn_1"
/>
(2)添加日历表,实现目标日期的课表查询,以下是代码实现和效果图

xml文件内容
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请选择查询日期"
android:textSize="20sp"
android:onClick="choosedate"
/>
java文件内容
public void choosedate(View v){
DatePickerDialog datepick=new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month=month+1;
date =year+"/"+month+"/"+dayOfMonth;
button2.setText(date);
}
},2019,11,22);
datepick.show();
dateclick=true;
}
(3)下拉框选目标课程进行查询,只要点击下拉框就会出现学生所有的课程名,点击查询就会查询得到该课程所有学生的成绩,以下是功能实现的代码和效果图
xml文件代码
<Spinner
android:id="@+id/spinner11"
android:layout_width="match_parent"
android:layout_height="50dp"
>
</Spinner>
java文件代码
spinner=(Spinner)findViewById(R.id.spinner11);
list=new ArrayList<String>();
list.add("chinese");
list.add("english");
list.add("math");
list.add("physics");
list.add("chemistry");
list.add("WW");
list.add("TQH");
arrayadapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
arrayadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayadapter);
Toast.makeText(this, "初始化", Toast.LENGTH_SHORT).show();
(4)添加Edittext组件来获取学生学号和班级名和课程名,来进行学生成绩查询,班级学生成绩查询,课程所有学生成绩查询,以下是代码和功能实现图
xml文件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级:"
android:textSize="20sp"
/>
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入班级"
android:textSize="20sp"
/>
java文件
private EditText editText;
editText=(EditText)findViewById(R.id.et_1);
public void click1(View v){
SQLiteDatabase db;
String et_1;
switch (v.getId()) {
case R.id.btn_1:
et_1 = editText.getText().toString();
Activity2.MyHelper myHelper = new Activity2.MyHelper(this);
db = myHelper.getReadableDatabase();
Toast.makeText(Activity6.this,"建立数据库对象",Toast.LENGTH_SHORT);
Cursor cursor = db.query("Student", null, "classes=?", new String[]{et_1 + ""}, null, null, null);
if(cursor.getCount()==0){
textView.setText("");
Toast.makeText(Activity6.this,"没有数据",Toast.LENGTH_SHORT);
}else{
if(cursor.moveToFirst()){
textView.setText("学号:"+cursor.getString(1)+"姓名:"+cursor.getString(2)+"班级:"+cursor.getString(3)+"英语:"+cursor.getString(4)+"语文"+cursor.getString(5));
}
while(cursor.moveToNext()){
textView.append("学号:"+cursor.getString(1)+"姓名:"+cursor.getString(2)+"班级:"+cursor.getString(3)+"英语:"+cursor.getString(4)+"语文"+cursor.getString(5));
}
}
cursor.close();
db.close();
break;
}
}
心得体会
这是自己第一次自己实现一个程序,还实现了这么多的功能,而且也做了几个比较好用的组件,算是从头开始做到这一步,从一开始的很多基础的东西都不是很懂,有很多疑问,到自己通过两个星期的学习,慢慢一步步实现所有的功能,遇到了很多的问题,列如数据库的创建和查询语句该怎么写才规范能够实现自己想要的效果,从Edittext组件传过来的数据该怎么获取,使用,还有利用日历表来选择自己想要的日期的组件,包括下拉框的实现都花费了自己很多的功夫,虽然现在做完了感觉这些看起来很简单,当时不懂得时候确实是摸着石头过河一步步走过来
这次也让我学会了很多的东西,自己也开始有了下一步的目标,就是要增加这个程序的功能,让自己再制作的过程中有更多的增进,也让这个软件更加的壮大
来源:CSDN
作者:从零开始的大学生
链接:https://blog.csdn.net/qq_41666522/article/details/103646119