activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学生管理系统" app:layout_constraintBottom_toTopOf="@+id/lv" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_weight="1" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/add" /> </androidx.constraintlayout.widget.ConstraintLayout>
dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/dept" android:hint="院系"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/grade" android:hint="院系"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/name" android:hint="院系"/> </LinearLayout>
list_view_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/dept" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/grade" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/name" /> </LinearLayout>
MainActivity.java
package com.liuli.sujvku; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { List<Student> students; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = findViewById(R.id.lv); StudentDao StudentDao = new StudentDao(this); students = StudentDao.findall(); StudentAdapter studentAdapter = new StudentAdapter(); lv.setAdapter(studentAdapter); TextView add = findViewById(R.id.add); add.setOnClickListener(view ->{ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("添加学生信息"); View v =View.inflate(this,R.layout.dialog,null); builder.setView(v); EditText dept = v.findViewById(R.id.dept); EditText grade = v.findViewById(R.id.grade); EditText name = v.findViewById(R.id.name); builder.setPositiveButton("保存",(dialogInterface,i)->{ Student student = new Student(); student.dept = dept.getText().toString(); student.grade = grade.getText().toString(); student.name = name.getText().toString(); StudentDao.addStudent(student); students.add(student); studentAdapter.notifyDataSetChanged(); }); builder.show(); }); } class StudentAdapter extends BaseAdapter{ @Override public int getCount() { return students.size(); } @Override public Student getItem(int i) { return students.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup){ if(view == null){ view = View.inflate(MainActivity.this,R.layout.list_view_item,null); } Student student = students.get(i); TextView dept = view.findViewById(R.id.dept); TextView grade = view.findViewById(R.id.grade); TextView name = view.findViewById(R.id.name); dept.setText(student.dept); dept.setText(student.grade); dept.setText(student.name); return view; } } }
StudentDBHelper.java
package com.liuli.sujvku; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class StudentDBHelper extends SQLiteOpenHelper { public StudentDBHelper(@Nullable Context context) { super(context,"stu.db",null,1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { String sql = "create table student (id Integer primary key autoincrement," + "dept varchar(20)," + "grade varchar(20)," + "name varchar(20))"; sqLiteDatabase.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
StudentDao
package com.liuli.sujvku; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; import java.util.List; public class StudentDao { SQLiteDatabase db = null; public StudentDao(Context ctx){ StudentDBHelper dBHelper =new StudentDBHelper(ctx); db = dBHelper.getReadableDatabase(); } public void addStudent(Student student){ String sql = "insert into student(dept,grade,name) values ('"+student.dept+"','"+student.grade+"','"+student.name+"')"; db.execSQL(sql); } public List<Student> findall(){ List<Student> stus = new ArrayList<>(); String sql = "select * from student"; Cursor cursor = db.rawQuery(sql,null); while(cursor.moveToNext()){ Student student = new Student(); student.dept = cursor.getString(0); student.grade = cursor.getString(1); student.name = cursor.getString(2); stus.add(student); } return stus; } }
Student
package com.liuli.sujvku; public class Student { public String dept; public String grade; public String name; }