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”
android: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;
最后
以前一直是自己在网上东平西凑的找,找到的东西也是零零散散,很多时候都是看着看着就没了,时间浪费了,问题却还没得到解决,很让人抓狂。
后面我就自己整理了一套资料,还别说,真香!
资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
t;
最后
以前一直是自己在网上东平西凑的找,找到的东西也是零零散散,很多时候都是看着看着就没了,时间浪费了,问题却还没得到解决,很让人抓狂。
后面我就自己整理了一套资料,还别说,真香!
资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。
[外链图片转存中…(img-NEyuxVLM-1725635311885)]
[外链图片转存中…(img-ISQ0Bwsf-1725635311886)]
[外链图片转存中…(img-vSGxVwwE-1725635311886)]
[外链图片转存中…(img-GWpGrxgZ-1725635311887)]
[外链图片转存中…(img-0SZuCdgQ-1725635311887)]
[外链图片转存中…(img-u7EYYG04-1725635311888)]
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0