- 创建DAO接口
@Dao
public interface StudentDao {
//增加
@Insert
void insertStudent(Student student);
//删除
@Delete
void deleteStudent(Student student);
//更新
@Update
void updateStudent(Student student);
//查询
@Query(“SELECT * FROM student”)
List getStudentList();
@Query(“SELECT * FROM student WHERE id = :id”)
Student getStudentById(int id);
}
- 创建数据库,MyDatabase继承RoomDatabase类
@Database(entities = {Student.class}, version = 1)
public abstract class MyDatabase extends RoomDatabase {
private static final String DATABASE_NAME = “student”;
private static MyDatabase databaseInstance;
public static synchronized MyDatabase getInstance(Context context) {
if (databaseInstance == null) {
databaseInstance = Room
.databaseBuilder(context.getApplicationContext(), MyDatabase.class, DATABASE_NAME)
.build();
}
return databaseInstance;
}
public abstract StudentDao studentDao();
}
- 初始化数据库
private void initDataBase() {
myDatabase = MyDatabase.getInstance(MainActivity.this);
new QueryStudentTask().execute();
}
- 现在先简单设计一下页面布局,页面布局主要用到ListView
(1)主页面activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<Button
android:id="@+id/btn_add"
android:layout_width=“200dp”
android:layout_height=“40dp”
android:layout_marginTop=“16dp”
android:background="@color/colorAccent"
android:onClick=“addStudent”
android:text=“添加学生”
android:textColor="@color/white"
android:textSize=“20sp”
app:layout_constraintHorizontal_bias=“0.497”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<ListView
android:id="@+id/lvStudent"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
app:layout_constraintTop_toBottomOf="@+id/btn_add"
android:layout_marginTop=“40dp”/>
</androidx.constraintlayout.widget.ConstraintLayout>
(2)item_student.xml
<?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=“wrap_content”
android:paddingTop=“12dp”
android:paddingBottom=“12dp”>
<TextView
android:id="@+id/tvId"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:gravity=“center”
android:layout_weight=“1”/>
<TextView
android:id="@+id/tvName"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:gravity=“center”
android:layout_weight=“1”/>
<TextView
android:id="@+id/tvAge"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:gravity=“center”
android:layout_weight=“1”/>
(3)增加学生和更新学生弹出框 dialog_layout_student.xml
<?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=“wrap_content”>
<EditText
android:id="@+id/etName"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
and
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
roid:hint=“姓名”
android:layout_weight=“1”/>
<EditText
android:id="@+id/etAge"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:hint=“年龄”
android:layout_weight=“1”/>
效果截图:
7. 我们主要实现的就是对学生的增删改查
这里我们我们用到AsyncTask,AsyncTask是安卓多线程的使用,AsyncTask抽象类如下:
public abstract class AsyncTask<Params, Progress, Result> {
…
}
// 类中参数为3种泛型类型
// 整体作用:控制AsyncTask子类执行线程任务时各个阶段的返回类型
// 具体说明:
// a. Params:开始异步任务执行时传入的参数类型,对应excute()中传递的参数
// b. Progress:异步任务执行过程中,返回下载进度值的类型
// c. Result:异步任务执行完成后,返回的结果类型,与doInBackground()的返回值类型保持一致
// 注:
// a. 使用时并不是所有类型都被使用
// b. 若无被使用,可用java.lang.Void类型代替
// c. 若有不同业务,需额外再写1个AsyncTask的子类
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
studentAdapter.notifyDataSetChanged();
}
其中onPostExecute这个方法是执行doInBackground()之后进行的
(1)增加学生
/**
- 插入学生信息
*/
private class InsertStudentTask extends AsyncTask<Void, Void, Void> {
String name;
String age;
public InsertStudentTask(final String name, final String age) {
this.name = name;
this.age = age;
}
@Override
protected Void doInBackground(Void… arg0) {
myDatabase.studentDao().insertStudent(new Student(name, age));
studentList.clear();
studentList.addAll(myDatabase.studentDao().getStudentList());
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
studentAdapter.notifyDataSetChanged();
}
}
这里主要的是
myDatabase.studentDao().insertStudent(new Student(name, age));
这是数据库对象通过studentDao接口中的insertStudent()方法进行插入数据。
(2)删除学生
/**
- 删除学生信息
*/
private class DeleteStudentTask extends AsyncTask<Void, Void, Void> {
Student student;
public DeleteStudentTask(Student student) {
this.student = student;
}
@Override
protected Void doInBackground(Void… arg0) {
myDatabase.studentDao().deleteStudent(student);
studentList.clear();
studentList.addAll(myDatabase.studentDao().getStudentList());
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
studentAdapter.notifyDataSetChanged();
}
}
这是数据库对象通过studentDao接口中的deleteStudent()方法进行删除数据。
(3)更新学生
/**
- 更新学生信息
*/
private class UpdateStudentTask extends AsyncTask<Void, Void, Void> {
int id;
String name;
String age;