Andriod --- JetPack (六):Room 增删改查,kotlin打包jar

首先创建实体类Student.java

package com.example.room;

import androidx.room.ColumnInfo;

import androidx.room.Entity;

import androidx.room.Ignore;

import androidx.room.PrimaryKey;

@Entity(tableName = “student”)

public class Student {

@PrimaryKey(autoGenerate = true)

@ColumnInfo(name = “id”, typeAffinity = ColumnInfo.INTEGER)

public int id;

@ColumnInfo(name = “name”, typeAffinity = ColumnInfo.TEXT)

public String name;

@ColumnInfo(name = “age”, typeAffinity = ColumnInfo.INTEGER)

public int age;

public Student(int id, String name, int age) {

this.id = id;

this.name = name;

this.age = age;

}

@Ignore

public Student(String name, int age) {

this.name = name;

this.age = age;

}

@Ignore

public Student(int id) {

this.id = id;

}

}

StudentDao.java

package com.example.room;

import androidx.room.Dao;

import androidx.room.Delete;

import androidx.room.Insert;

import androidx.room.Query;

import androidx.room.Update;

import java.util.List;

@Dao

public interface StudentDao {

@Insert

void insertStudent(Student… students); // 可以传入多个 Student

@Delete

void deleteStudent(Student… students);

@Update

void updateStudent(Student… students);

@Query(“SELECT * FROM student”)

List getAllStudent();

@Query(“SELECT * FROM student WHERE id = :id”)

List getStudentById(Integer id);

}

MyDataBase.java

package com.example.room;

import android.content.Context;

import androidx.room.Database;

import androidx.room.Room;

import androidx.room.RoomDatabase;

// 一定是抽象类

@Database(entities = {Student.class}, version = 1, exportSchema = false)

public abstract class MyDataBase extends RoomDatabase {

private static final String DATABASE_NAME = “my_db.db”;

private static MyDataBase mInstance;

// 构建 database 单例

public static synchronized MyDataBase getInstance(Context context) {

if(mInstance == null) {

mInstance = Room.databaseBuilder(context.getApplicationContext(), MyDataBase.class, DATABASE_NAME)

//.allowMainThreadQueries() // 允许在主线程操作数据库

.build();

}

return mInstance;

}

public abstract StudentDao getStudentDao();

}

StudentRecycleViewAdapter.java

package com.example.room;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class StudentRecycleViewAdapter extends RecyclerView.Adapter<StudentRecycleViewAdapter.MyViewHolder> {

List students;

public void setStudents(List students) {

this.students = students;

}

public StudentRecycleViewAdapter(List students) {

this.students = students;

}

@NonNull

@Override

public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent, false);

return new MyViewHolder(root);

}

@Override

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

Student student = students.get(position);

holder.tvId.setText(String.valueOf(student.id));

holder.name.setText(student.name);

holder.age.setText(String.valueOf(student.age));

}

@Override

public int getItemCount() {

return students == null ? 0 :students.size();

}

public class MyViewHolder extends RecyclerView.ViewHolder {

private TextView tvId, name, age;

public MyViewHolder(View view) {

super(view);

tvId = view.findViewById(R.id.tvId);

name = view.findViewById(R.id.name);

age = view.findViewById(R.id.age);

}

}

}

MainActivity.java

package com.example.room;

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

private StudentRecycleViewAdapter adapter;

private StudentDao studentDao;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

List students = new ArrayList<>();

RecyclerView recyclerView = findViewById(R.id.rv_01);

adapter = new StudentRecycleViewAdapter(students);

recyclerView.setLayoutManager(new LinearLayoutManager(this));

recyclerView.setAdapter(adapter);

MyDataBase myDataBase = MyDataBase.getInstance(this);

studentDao = myDataBase.getStudentDao();

}

// Room 不允许在主线程操作数据库,如果非在主线程操作数据库,请修改 MyDataBase 文件

// 增加

public void mInsert(View view) {

Student s1 = new Student(“Jack”, 20);

Student s2 = new Student(“Rose”, 22);

new InsertStudentTask(studentDao).execute(s1,s2);

}

// 删除

public void mDelete(View view) {

Student s1 = new Student(1);

new DeleteStudentTask(studentDao).execute(s1);

}

// 修改

public void mUpdate(View view) {

Student s1 = new Student(3, “Jason”, 21);

new UpdateStudentTask(studentDao).execute(s1);

}

// 查询

public void mQuery(View view) {

new GetAllStudentTask(studentDao).execute();

}

// 异步线程添加学生

class InsertStudentTask extends AsyncTask<Student, Void, Void> {

private StudentDao studentDao;

public InsertStudentTask(StudentDao studentDao) {

this.studentDao = studentDao;

}

@Override

protected Void doInBackground(Student… students) {

studentDao.insertStudent(students);

return null;

}

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

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

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

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

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

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长。而不成体系的学习效果低效漫长且无助。时间久了,付出巨大的时间成本和努力,没有看到应有的效果,会气馁是再正常不过的。

所以学习一定要找到最适合自己的方式,有一个思路方法,不然不止浪费时间,更可能把未来发展都一起耽误了。

如果你是卡在缺少学习资源的瓶颈上,那么刚刚好我能帮到你。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
,真正体系化!**

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

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-tu0uL2s7-1712601864448)]

最后

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长。而不成体系的学习效果低效漫长且无助。时间久了,付出巨大的时间成本和努力,没有看到应有的效果,会气馁是再正常不过的。

所以学习一定要找到最适合自己的方式,有一个思路方法,不然不止浪费时间,更可能把未来发展都一起耽误了。

如果你是卡在缺少学习资源的瓶颈上,那么刚刚好我能帮到你。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-VDTch0Fl-1712601864449)]

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值