package com.cqjtu.studentsystem;
import java.io.Serializable;
import java.util.List;
public class Student implements Serializable {
private String name;
private String id;
private String major;
private String age;
private String sex;
private String time;
private List<String> course;
private String college;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Student(String id, String name, String sex, String age, String time, String major, String college, List<String> course) {
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
this.time=time;
this.major=major;
this.college=college;
this.course=course;
}
public Student(){
}
public List<String> getCourse() {
return course;
}
public void setCourse(List<String> course) {
this.course = course;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getName(){
return name;
}
public String getMajor(){
return major;
}
public void setName(String name){
this.name=name;
}
public void setMajor(String major){
this.major=major;
}
}
以上的定义包括学号、姓名、性别、年龄、专业、入学时间、学院以及所选择的课程
在同样编写java代码的位置创建名为MySQLite的类,用来建立数据库和学生信息表:
package com.cqjtu.studentsystem;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MySQLite extends SQLiteOpenHelper {
private static final String TABLE_NAME ="student1" ;
private Context context;
public MySQLite(@Nullable Context context,int version) {
super(context, "student.db", null, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table student1("+
"id text," +
"name text," +
"age text," +
"sex text," +
"time datatype," +
"major text," +
"college text," +
"course text);");
Toast.makeText(context,"表成功创建",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
学生信息表的属性应该和Student类中的属性一致!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="270dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/student1"
android:layout_weight="1"
android:id="@+id/student_image"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:layout_toRightOf="@id/student_image"
android:text="李易峰"
android:textSize="20sp"
android:id="@+id/student_name"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="@id/student_name"
android:text="性别"
android:gravity="bottom"
android:textSize="15sp"
android:id="@+id/student_sex"/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="@id/student_sex"
android:text="年龄"
android:gravity="bottom"
android:textSize="15sp"
android:id="@+id/student_age"/>
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="专业"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/student_image"
android:layout_marginTop="30dp"
android:id="@+id/student_major"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="入学时间"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/student_major"
android:layout_marginTop="30dp"
android:id="@+id/student_time"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:src="@drawable/edit"
android:focusable="false"
android:id="@+id/student_edit"/>
<ImageView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="39dp"
android:src="@drawable/delete"
android:focusable="false"
android:id="@+id/student_delete"/>
</RelativeLayout>
</LinearLayout>
在java代码编写的文件下创建一个名为StuAdapter的适配器,用来匹配我们设计的学生信息列表格式
package com.cqjtu.studentsystem;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class StuAdapter extends BaseAdapter implements View.OnClickListener {
private List <Student>stuDates;
private Context stuContext;
private InnerItemOnClickListener myListener;
public StuAdapter(Context context,List<Student>list){
this.stuContext=context;
this.stuDates=list;
}
@Override
public int getCount() {
return stuDates.size();
}
@Override
public Object getItem(int position) {
return stuDates.get(position);
}
@Override
public long getItemId(int position) {
Student student=(Student)stuDates.get(position);
return position;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View stuView=View.inflate(stuContext,R.layout.myspinner_item,null);
TextView stuName=stuView.findViewById(R.id.student_name);
TextView stuSex=stuView.findViewById(R.id.student_sex);
TextView stuAge=stuView.findViewById(R.id.student_age);
TextView stuTime=stuView.findViewById(R.id.student_time);
TextView stuMajor=stuView.findViewById(R.id.student_major);
ImageView edit=stuView.findViewById(R.id.student_edit);
ImageView delete=stuView.findViewById(R.id.student_delete);
Student student=(Student)stuDates.get(i);
stuName.setText(student.getName());
stuSex.setText(student.getSex());
stuAge.setText(student.getAge());
stuTime.setText(student.getTime());
stuMajor.setText(student.getMajor());
edit.setImageResource(R.drawable.edit);
delete.setImageResource(R.drawable.delete);
edit.setOnClickListener(this);
delete.setOnClickListener(this);
edit.setTag(i);
delete.setTag(i);
return stuView;
}
interface InnerItemOnClickListener {
abstract void itemClick(View view);
}
public void setOnInnerItemOnClickListener(InnerItemOnClickListener listener){
this.myListener=listener;
}
@Override
public void onClick(View view) {
myListener.itemClick(view);
}
}
if(view.getId()==R.id.addname){
String id=id1.getText().toString();
String name=name1.getText().toString();
String sex=getRadio(sex1);
String age=age1.getText().toString();
String time=time1.getText().toString();
String major=major1.getText().toString();
String college=college1.getSelectedItem().toString();
List<String>course=getCheckValue(course1);
if (isEmpty(name)) {
Toast.makeText(MainActivity.this,"姓名不能为空",Toast.LENGTH_SHORT).show();
return;
}
if (isEmpty(age)){
Toast.makeText(MainActivity.this,"年龄不能为空",Toast.LENGTH_SHORT).show();
return;
}
if (isEmpty(sex)) {
Toast.makeText(MainActivity.this,"请选择性别",Toast.LENGTH_SHORT).show();
return;
}
if (isEmpty(time)) {
Toast.makeText(MainActivity.this,"请输入入学年份",Toast.LENGTH_SHORT).show();
return;
}
if (isEmpty(major)) {
Toast.makeText(MainActivity.this,"专业不能为空",Toast.LENGTH_SHORT).show();
return;
}
if (isEmpty(college)){
Toast.makeText(MainActivity.this,"学院不能为空",Toast.LENGTH_SHORT).show();
return;
}
if (isEmpty(course)){
Toast.makeText(MainActivity.this,"请勾选课程",Toast.LENGTH_SHORT).show();
return;
}
Student student=new Student(id,name,sex,age,time,major,college,course);
students.add(student);
mySQLite=new MySQLite(this,1);
db= mySQLite.getWritableDatabase();
//使用ContentValues添加数据
ContentValues values=new ContentValues();
values.put("id",id);
values.put("name",name);
values.put("sex",sex);
values.put("age",age);
values.put("time",time);
values.put("major",major);
values.put("college",college);
values.put("course",listToString(course));
db.insert(TABLE_NAME, null, values);
db.close();
sent("数据库的学生信息刷新啦!");
Toast.makeText(this,"已经添加"+students.size()+"条记录",Toast.LENGTH_SHORT).show();
i=i+1;
id1.setText("1000"+i);
name1.setText("");
sex1.clearCheck();
age1.setText("");
time1.setText("");
major1.setText("");
showSpanner(college1,"信息科学与工程");
box1.setChecked(false);
box2.setChecked(false);
box3.setChecked(false);
box4.setChecked(false);
name1.setFocusable(true);
name1.setFocusableInTouchMode(true);
name1.requestFocus();
}else if(view.getId()==R.id.addmajor){
intent.setClass(this,display.class);
intent.putExtra("students",(Serializable)students);
startActivity(intent);
以上包括添加到lis里面和数据库里面
1、删除学生信息效果图如下:
红色箭头处:
2、删除学生信息的代码如下:
@Override
public void itemClick(View view){
Intent intent=new Intent();
switch (view.getId()){
case R.id.student_delete:
int k=(Integer) view.getTag();
deleteStudent(k);
students.remove(k);
i=i-1;
id1.setText("1000"+i);
stuAdapter.notifyDataSetChanged();
sent("您的数据库删除了一条学生信息哦!");
Toast.makeText(display.this,"删除成功",Toast.LENGTH_SHORT).show();
break;
}
}
学生信息在数据库中的删除如下:
public void deleteStudent(int i){
mySQLite=new MySQLite(this,1);
db=mySQLite.getWritableDatabase();
db.delete("student1","id=?",new String[]{students.get(i).getId()});
db.close();
}
1、修改学生信息的效果如下:
红色箭头处:
修改学生信息的页面如下所示:
2、修改学生信息的代码如下:
@Override
public void onClick(View view) {
if(view.getId()==R.id.quit){
finish();
}else if (view.getId()==R.id.save){
String id=id22.getText().toString();
students.get(k()).setId(id);
String name=name22.getText().toString();
students.get(k()).setName(name);
String sex=getRadio(sex22);
students.get(k()).setSex(sex);
String age=age22.getText().toString();
students.get(k()).setAge(age);
String time=time22.getText().toString();
students.get(k()).setTime(time);
String major=major22.getText().toString();
students.get(k()).setMajor(major);
String college=college22.getSelectedItem().toString();
students.get(k()).setCollege(college);
List<String>course=getCheckValue(course22);
students.get(k()).setCourse(course);
mySQLite=new MySQLite(this,1);
db=mySQLite.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",name);
values.put("sex",sex);
values.put("age",age);
values.put("time",time);
values.put("major",major);
values.put("college",college);
values.put("course", listToString(course));
db.update(TABLE_NAME,values,"id=?",new String[]{id});
sent("数据库的一条学生信息修改成功啦!");
stuAdapter.notifyDataSetChanged();
finish();
}
}
1、查询学生信息的效果展示:
查询信息的页面展示如下:
2、查询信息java代码如下:
查询所有学生的信息代码如下:
public void searchAll(Student student) {
MySQLite mySQLite = new MySQLite(this, 1);
SQLiteDatabase database = mySQLite.getReadableDatabase();
Cursor cursor = database.rawQuery("select * from student1", null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
String time = cursor.getString(cursor.getColumnIndex("time"));
String major = cursor.getString(cursor.getColumnIndex("major"));
String college = cursor.getString(cursor.getColumnIndex("college"));
String course = cursor.getString(cursor.getColumnIndex("course"));
student = new Student(id, name, sex, age, time, major, college, stringToList(course)); //student_info存一个条目的数据
students.add(student);
i=students.size();
}
}
关键字查询代码如下:
String key=find1.getText().toString();
if (isEmpty(key)) {
Toast.makeText(this,"查找条件不能为空",Toast.LENGTH_SHORT).show();
return;
}
List<Student>find=new ArrayList<Student>();
mySQLite=new MySQLite(this,1);
String selectQuery="SELECT * FROM student1 where name like '%"+key+"%' or major like '%"+key+"%' or id like '%"+key+"%' or age like '%"+key+"%' " +
"or sex like '%"+key+"%' or time like '%"+key+"%' or college like '%"+key+"%' or course like '%"+key+"%'";
SQLiteDatabase db=mySQLite.getReadableDatabase();
Cursor cursor=db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
do{
Student student=new Student();
student.setId(cursor.getString(cursor.getColumnIndex("id")));
student.setName(cursor.getString(cursor.getColumnIndex("name")));
student.setSex(cursor.getString(cursor.getColumnIndex("sex")));
student.setAge(cursor.getString(cursor.getColumnIndex("age")));
student.setTime(cursor.getString(cursor.getColumnIndex("time")));
student.setMajor(cursor.getString(cursor.getColumnIndex("major")));
student.setCollege(cursor.getString(cursor.getColumnIndex("college")));
student.setCourse(stringToList(cursor.getString(cursor.getColumnIndex("course"))));
find.add(student);
}while(cursor.moveToNext());
}
intent.setClass(this,search.class);
intent.putExtra("find",(Serializable)find);
startActivity(intent);
}
判断字符串不能为空如下:
private boolean isEmpty(String str) {
return str.length()==0||null==str;
}
判断列表不能为空(课程)如下:
private boolean isEmpty(List<String> str){
return str.size()==0||null==str;
}
1、字符串转list:
private List<String> stringToList(String course){
ArrayList<String> course1 = new ArrayList<String>(Arrays.asList(course.split(",")));
return course1;
}
2、list转字符串:
private String listToString(List<String>course){
String[] course1= new String[course.size()];
course1 = course.toArray(course1);
String course2="";
for (int i=0;i< course1.length;i++){
course2=course2+","+course1[i];
}
return course2;
}
public String getRadio(RadioGroup radioGroup){
String info="";
int num=radioGroup.getChildCount();
for(int i=0;i<num;i++){
RadioButton rd=(RadioButton)radioGroup.getChildAt(i);
if(rd.isChecked()){
info=rd.getText().toString();
break;
}
}
return info;
}
public List<String> getCheckValue(LinearLayout linearLayout) {
List<String> options = new ArrayList<String>();
int num = linearLayout.getChildCount();
for (int i = 0; i < num; i++) {
CheckBox checkBox = (CheckBox) linearLayout.getChildAt(i);
if (checkBox.isChecked()) {
options.add(checkBox.getText().toString());
}
}
return options;
}
学院下拉框展示的内容如下:
private void initSpinner(){
String college[]={"信息科学与工程","人文学院","经济管理学院","外国语学院","土木工程学院"};
ArrayAdapter<String>arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,college);
college1.setAdapter(arrayAdapter);
}
获取下拉框的值如下:
public void showSpanner(Spinner spinner,String values){
SpinnerAdapter adapter=spinner.getAdapter();
int k=adapter.getCount();
for(int i=0;i<k;i++){
if(values.equals(adapter.getItem(i).toString())){
spinner.setSelection(i,true);
break;
}
}
}
private void getDate(){
cal=Calendar.getInstance();
year=cal.get(Calendar.YEAR);
month=cal.get(Calendar.MONTH);
day=cal.get(Calendar.DAY_OF_MONTH);
}
以上就是本次博客学生成绩管理系统的部分内容啦,觉得可以的,可以下载学长上传的资料,里面有整个项目的源代码,保证能够成功运行哦!感谢大家查看、记得留言评论点赞哦!