Room+ViewModel+LiveData

Room框架支持的LiveData会自动监听数据库的变化,当数据库发生变化的时候,会调用onChanged函数更新UI 

1.MainActivity

package com.tiger.room2;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private StudentRecyclerViewAdapter adapter;
    private StudentViewModel studentViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recycleView = findViewById(R.id.recycleView);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        List<Student> students = new ArrayList<>();
        adapter = new StudentRecyclerViewAdapter(students);
        recycleView.setAdapter(adapter);
        studentViewModel = new ViewModelProvider(this).get(StudentViewModel.class);
        //自动更新,给LiveData 添加监听
        LiveData<List<Student>> listLiveData = studentViewModel.queryAllStudentsLive();
        List<Student> value = listLiveData.getValue();
        if (value==null) {
            Log.d("ning","value is null");
        }else {
            Log.d("ning",value.toString());
        }

        listLiveData.observe(this, new Observer<List<Student>>() {
            @Override
            public void onChanged(List<Student> students) {
                Log.d("ning",listLiveData.getValue().toString());
                adapter.setStudents(students);
                adapter.notifyDataSetChanged();
            }
        });
    }

    public void mInsert(View view) {
        Student jack = new Student("Jack", 20);
        Student rose = new Student("rose", 23);
        studentViewModel.insertStudent(jack, rose);
    }

    public void mDelete(View view) {
        Student student = new Student(1);
        studentViewModel.deleteStudent(student);
    }

    public void mUpdate(View view) {
        Student rose = new Student(3, "wqeqasdaddqweqwe", 212313);
        studentViewModel.updateStudent(rose);
    }

    public void mClear(View view) {
        studentViewModel.deleteAllStudent();
    }

}

2.MyDataBase

package com.tiger.room2;

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;


    public static MyDataBase getInstance(Context context){

        if (mInstance == null){
            synchronized(MyDataBase.class){
                if (mInstance == null){
                    mInstance = Room.databaseBuilder(context.getApplicationContext(), MyDataBase.class,DATABASE_NAME)
//                            .allowMainThreadQueries() 允许主线程操作数据库
                            .build();
                }
            }

        }
        return mInstance;
    }

    public abstract StudentDao getStudentDao();//room自动实现

}

3.Student

package com.tiger.room2;

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;


//    @Ignore
//    public boolean flag; 属性也可以忽略 ,代表它不是数据库的一列

    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Ignore //room忽略此构造方法 构造对象
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Ignore
    public Student(int id) {
        this.id = id;
    }


    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

4.StudentDao

package com.tiger.room2;

import androidx.lifecycle.LiveData;
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 insert(Student... students);

    @Delete
    void deleteStudent(Student... students);

    @Query("delete from student")
    void deleteAll();

    @Update
    void updateStudent(Student... students);

    @Query("select * from student")
    LiveData<List<Student>> getAllStudentsLive();


    @Query("select * from student where id = :id")
    Student getStudentById(int id);


}

5.StudentRecyclerViewAdapter

package com.tiger.room2;

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;

/**
 * @author ningchuanqi
 * @version V1.0
 */
public class StudentRecyclerViewAdapter extends RecyclerView.Adapter {

    List<Student> students;

    public StudentRecyclerViewAdapter(List<Student> students) {
        this.students = students;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
       return new MyViewHoder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Student student = students.get(position);
        TextView tvId = holder.itemView.findViewById(R.id.tvId);
        tvId.setText(String.valueOf(student.id));

        TextView tvName = holder.itemView.findViewById(R.id.tvName);
        tvName.setText(student.name);

        TextView tvAge = holder.itemView.findViewById(R.id.tvAge);
        tvAge.setText(String.valueOf(student.age));
    }

    @Override
    public int getItemCount() {
        return students.size();
    }

    static class MyViewHoder extends RecyclerView.ViewHolder {

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}

6.StudentReposity

package com.tiger.room2;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;

import androidx.lifecycle.LiveData;

import java.util.List;

public class StudentRepository {

    private StudentDao studentDao;

    public StudentRepository(Context context) {
        MyDataBase dataBase = MyDataBase.getInstance(context);
        this.studentDao = dataBase.getStudentDao();
    }


    public void insertStudent(Student ... students) {
        new InsertStudentTask(studentDao).execute(students);
    }


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

        private StudentDao studentDao;

        public InsertStudentTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student ... students) {
            studentDao.insert(students);
            return null;
        }
    }


    public void updateStudent(Student... students) {
        new UpdateStudentTask(studentDao).execute(students);
    }

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

        private StudentDao studentDao;

        public UpdateStudentTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.updateStudent(students);
            return null;
        }
    }

//除非服务器崩了,要不然代码不可能错

    public void deleteStudent(Student ... student) {
        new DeleteStudentTask(studentDao).execute(student);
    }

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

        private StudentDao studentDao;

        public DeleteStudentTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.deleteStudent(students);
            return null;
        }
    }

    public void deleteAllStudent() {
        new DeleteAllStudentTask(studentDao).execute();
    }

    class DeleteAllStudentTask extends AsyncTask<Void, Void, Void> {

        private StudentDao studentDao;

        public DeleteAllStudentTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }


        @Override
        protected Void doInBackground(Void... voids) {
            studentDao.deleteAll();
            return null;
        }
    }

    public LiveData<List<Student>> queryAllStudentsLive() {
        return studentDao.getAllStudentsLive();
    }

//    class GetAllStudentTask extends AsyncTask<Void,Void,Void>{
//
//        private StudentDao studentDao;
//
//        public GetAllStudentTask(StudentDao studentDao) {
//            this.studentDao = studentDao;
//        }
//
//        @Override
//        protected Void doInBackground(Void... voids) {
//            List<Student> students = studentDao.getAllStudent();
//            adapter.setStudents(students);
//            return null;
//        }
//
//        @Override
//        protected void onPostExecute(Void aVoid) {
//            super.onPostExecute(aVoid);
//            adapter.notifyDataSetChanged();
//        }
//    }


}

7.StudentViewModel

package com.tiger.room2;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.List;

public class StudentViewModel extends AndroidViewModel {

    private StudentRepository studentRepository;

    public StudentViewModel(@NonNull Application application) {
        super(application);
        this.studentRepository = new StudentRepository(application);
    }

    public void insertStudent(Student ... students){
        studentRepository.insertStudent(students);
    }
    public void deleteStudent(Student ... students){
        studentRepository.deleteStudent(students);
    }
    public void updateStudent(Student ... students){
        studentRepository.updateStudent(students);
    }

    public void deleteAllStudent(){
        studentRepository.deleteAllStudent();
    }

    public LiveData<List<Student>>  queryAllStudentsLive(){
        return studentRepository.queryAllStudentsLive();
    }

}

8.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">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.709"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加"
        android:onClick="mInsert"
        app:layout_constraintBottom_toTopOf="@+id/guideline5"
        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"
        android:onClick="mDelete"
        app:layout_constraintBottom_toTopOf="@+id/guideline5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改"
        android:onClick="mUpdate"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline5" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清空"
        android:onClick="mClear"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline5" />
</androidx.constraintlayout.widget.ConstraintLayout>

9.item.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="wrap_content"
    android:paddingVertical="10dip">

    <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:text="1"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:text="Jack"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:text="18"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />
</androidx.constraintlayout.widget.ConstraintLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/451721.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

红帽认证RHCE好考吗?多长时间能考下来?报名费多少一门?哪些人适合考红帽认证?

一、红帽认证等级 红帽认证考试有三个等级&#xff0c;分别是RHCSA&#xff08;红帽认证系统管理员&#xff09;&#xff0c;RHCE&#xff08;红帽认证工程师&#xff09;&#xff0c;RHCA&#xff08;红帽认证架构师&#xff09;。RHCA是最高级别的认证。 二、RHCE考试 1、考…

一款好用的AI工具——边界AICHAT(二)

目录 3.11、AI智能在线抠图3.12、AI智能图片增强放大3.13、AI图片擦除3.14、AI图片理解3.15、音频视频网页理解模型3.16、角色扮演3.17、AI文档理解对话3.18、公文写作模式3.19、插件库3.20、AI思维导图3.21、PPT一键生成3.22、音视频生成PPT 本篇博文接上一篇博文 一款好用的…

Singularity(四)| 自定义容器

Singularity&#xff08;四&#xff09;| 自定义容器 4.1 Singularity Definition 文件 对于可复制的、高质量的容器&#xff0c;我们应该使用定义文件&#xff08;Definition File&#xff09;构建 Singularity 容器 。使用定义文件的方式可以在纯文本文件中描述容器的配置和…

数据集踩的坑及解决方案汇总

数据集踩的坑及解决方案汇总 数据集各种格式构建并训练自己的数据集汇总Yolo系列SSDMask R-CNN报错 NotADirectoryError: [Errno 20] Not a directory: /Users/mia/Desktop/P-Clean/mask-RCNN/PennFudanPed2/labelme_json/.DS_StoreFaster R-CNN数据的格式转换划分数据集设定内…

移掉 K 位数字(LeetCode 402)

文章目录 1.问题描述2.难度等级3.热门指数4.解题思路4.1 暴力法4.2 贪心 单调栈 参考文献 1.问题描述 给你一个以字符串表示的非负整数 num 和一个整数 k&#xff0c;移除这个数中的 k 位数字&#xff0c;使得剩下的整数最小。请你以字符串形式返回这个最小的整数。 示例 1 …

进电子厂了,感触颇多...

作者&#xff1a;三哥 个人网站&#xff1a;https://j3code.cn 本文已收录到语雀&#xff1a;https://www.yuque.com/j3code/me-public-note/lpgzm6y2nv9iw8ec 是的&#xff0c;真进电子厂了&#xff0c;但主人公不是我。 虽然我不是主人公&#xff0c;但是我经历的过程是和主…

Igraph入门指南 6

3、make_系列&#xff1a;igraph的建图工具 按照定义&#xff0c;正则图是指各顶点的度均相同的无向简单图&#xff0c;因为我目前没有找到描述度相等的有向&#xff08;或自环图&#xff09;的标准名称&#xff0c;所以在本文中借用一下这个概念&#xff0c;并加上定语有向无…

Android studio SDK Manager显示不全的问题解决

发现SDK Manager中只显示已下载的SDK版本&#xff0c;想下载其他版本下载不到&#xff0c;尝试翻墙也没用&#xff0c;修改host文件成功 在多个地点Ping服务器,网站测速 - 站长工具 输入dl.google.com&#xff0c;进行ping检测。 选择一个地址&#xff0c;比如180.163.150.1…

【深度学习笔记】5_12稠密连接网络(DenseNet)

注&#xff1a;本文为《动手学深度学习》开源内容&#xff0c;部分标注了个人理解&#xff0c;仅为个人学习记录&#xff0c;无抄袭搬运意图 5.12 稠密连接网络&#xff08;DenseNet&#xff09; ResNet中的跨层连接设计引申出了数个后续工作。本节我们介绍其中的一个&#xf…

Python学习:基础语法

版本查看 python --version编码 默认情况下&#xff0c;Python 3 源码文件以 UTF-8 编码&#xff0c;所有字符串都是 unicode 字符串。 特殊情况下&#xff0c;也可以为源码文件指定不同的编码&#xff1a; # -*- coding: cp-1252 -*-标识符 第一个字符必须是字母表中字母或…

Java学习笔记------常用API

Math类 常用方法&#xff1a; 1. publicb static int abs(int a) 获取参数绝对值 2. publicb static double ceil(double a) 向上取整 3. publicb static floor(double a) 向下取整 4.public static int round(float a) 四舍五入 5. publicb static int max…

Vue3全家桶 - VueRouter - 【2】重定向路由

重定向路由 在路由规则数组中&#xff0c;可采用 redirect 来重定向到另一个地址&#xff1a; 通常是将 / 重定向到 某个页面&#xff1b; 示例展示&#xff1a; router/index.js&#xff1a;import { createRouter, createWebHashHistory, createWebHistory } from vue-route…

云桥通SDWAN企业组网的15大应用场景

云桥通SD-WAN企业组网技术在企业网络中有多样化的应用场景&#xff0c;在技术不断迭代升级中&#xff0c;已经越来越匹配现在的互联网环境&#xff0c;其中在这15中常见的应用场景中&#xff0c;使用云桥通SDWAN企业组网可以很好的帮到企业&#xff1a; 分支机构连接优化&#…

蓝桥杯之【01背包模版】牛客例题展示

牛客链接 #include <bits/stdc.h> using namespace std; int n,V; const int N1010; int v[N],w[N]; int dp[N][N]; int main() {cin>>n>>V;for(int i1;i<n;i){cin>>v[i]>>w[i];}for(int i1;i<n;i){for(int j1;j<V;j){dp[i][j]dp[i-1][…

力扣刷题Days16(js)-67二进制求和

目录 1,题目 2&#xff0c;代码 2.1转换进制数 2.2模拟加法 3&#xff0c;学习与总结 Math.floor() 模拟加法思路回顾 重点复习巩固 模拟加法的思路和学习位运算&#xff1b; 今天没精力了&#xff0c;先休息 1,题目 给你两个二进制字符串 a 和 b &#xff0c;以二进制…

CSS 用 flex 布局绘制骰子

<!DOCTYPE html> <html><head><meta charset"utf-8"><title></title><style>.box {height: 100px;width: 100px;border: 2px solid grey;border-radius: 10px;display: flex;justify-content: center; // 水平居中/* alig…

桥接模式以及在JDBC源码剖析

介绍&#xff1a; 1、桥接模式是指&#xff1a;将实现和抽象放在两个不同类层次中&#xff0c;使两个层次可以独立改变 2、是一种结构型设计模式 3、Bridge模式基于类的最小设计原则&#xff0c;通过使用封装、聚合以及继承等行为让不同的类承担不同的职责。 4、特点&#xff1…

【DAY11 软考中级备考笔记】数据结构 查找和排序

数据结构 查找和排序 3月12日 – 天气&#xff1a;晴 1. 顺序查找 顺序查找就是简单的从头一个一个的进行比较&#xff0c;注意它的平均查找长度 2. 折半查找 折半查找和二叉排序树一致&#xff1a; 优点&#xff1a;查找效率很高 缺点&#xff1a;要求必须是循序存储并且表中…

LoadRunner学习:RuntimeSetting、参数化、关联、(unfinished

LoadRunner RuntimeSetting 运行时设置 在Vuser中设置Run-time Settings RunLogic&#xff1a;运行逻辑&#xff0c;决定了脚本真正执行逻辑&#xff0c; Init和End部分代码只能执行一次。决定脚本真正执行逻辑的意思是&#xff0c;在Run中的代码和Number of Iteration决定了…