}

}

创建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

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”>

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” />

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”/>

(2)item_student.xml

android:orientation=“horizontal”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:paddingTop=“12dp”

android:paddingBottom=“12dp”>

android:id=“@+id/tvId”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:gravity=“center”

android:layout_weight=“1”/>

android:id=“@+id/tvName”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:gravity=“center”

android:layout_weight=“1”/>

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

android:orientation=“horizontal”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

android:id=“@+id/etName”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:hint=“姓名”

android:layout_weight=“1”/>

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 {

}

// 类中参数为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 {

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 {

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)更新学生 自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

《设计思想解读开源框架》

第一章、 热修复设计

第一节、 AOT/JIT & dexopt 与 dex2oat 第二节、 热修复设计之 CLASS_ISPREVERIFIED 问题 第三节、热修复设计之热修复原理 第四节、Tinker 的集成与使用(自动补丁包生成) 第二章、 插件化框架设计 第一节、 Class 文件与 Dex 文件的结构解读 第二节、 Android 资源加载机制详解 第三节、 四大组件调用原理 第四节、 so 文件加载机制 第五节、 Android 系统服务实现原理 第三章、 组件化框架设计 第一节、阿里巴巴开源路由框——ARouter 原理分析 第二节、APT 编译时期自动生成代码&动态类加载 第三节、 Java SPI 机制 第四节、 AOP&IOC 第五节、 手写组件化架构 第四章、图片加载框架 第一节、图片加载框架选型 第二节、Glide 原理分析 第三节、手写图片加载框架实战 第五章、网络访问框架设计 第一节、网络通信必备基础 第二节、OkHttp 源码解读 第三节、Retrofit 源码解析 第六章、 RXJava 响应式编程框架设计 第一节、链式调用 第二节、 扩展的观察者模式 第三节、事件变换设计 第四节、Scheduler 线程控制 第七章、 IOC 架构设计 第一节、 依赖注入与控制反转 第二节、ButterKnife 原理上篇、中篇、下篇 第三节、Dagger 架构设计核心解密 第八章、 Android 架构组件 Jetpack 第一节、 LiveData 原理 第二节、 Navigation 如何解决 tabLayout 问题 第三节、 ViewModel 如何感知 View 生命周期及内核原理 第四节、 Room 架构方式方法 第五节、 dataBinding 为什么能够支持 MVVM 第六节、 WorkManager 内核揭秘 第七节、 Lifecycles 生命周期 本文包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

第一节、 依赖注入与控制反转 第二节、ButterKnife 原理上篇、中篇、下篇 第三节、Dagger 架构设计核心解密 [外链图片转存中…(img-oI7k8b0T-1713082602661)] 第八章、 Android 架构组件 Jetpack 第一节、 LiveData 原理 第二节、 Navigation 如何解决 tabLayout 问题 第三节、 ViewModel 如何感知 View 生命周期及内核原理 第四节、 Room 架构方式方法 第五节、 dataBinding 为什么能够支持 MVVM 第六节、 WorkManager 内核揭秘 第七节、 Lifecycles 生命周期 [外链图片转存中…(img-GkgFPCzn-1713082602662)] 本文包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中… [外链图片转存中…(img-pvVS6wci-1713082602662)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

文章来源

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: