学习使用Room构建一个简单的汽车信息管理App;练练手和回顾之前的RecyclerView的知识要点。
Demo放置位置可以自己下载来研究研究,比较简单。

简述

这个Demo主要是包含一下三个知识点

  1. Room的应用
  2. Recycler View的应用
  3. Dialog自定义应用

Room导入过程中

在根据网上的资料进行编写代码的时候不知到是哪一步有问题,我的app连初始化都不行。Cause: java.lang.ExceptionInInitializerError应该是编译问题

  • What went wrong:
    Execution failed for task ‘:app:compileDebugJavaWithJavac’.
    java.lang.ExceptionInInitializerError

这个寻求大神帮助换了一个依赖就可以了,我也不知道为什么?

implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

基本流程

  1. 依赖导入
  2. 构建实体类,类似javaBean的
package com.example.roomdatabasetest;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "Cars")
public class Car {
    //这个是JavaBean 类
    //设置主键自动生成
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "car_id")
    private int id;
    @ColumnInfo(name = "car_brand")
    private String brand;
    @ColumnInfo(name = "car_price")
    private int price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}
  1. 构建数据库(以上基本都是通过注释进行的)
  2. 单例模式进行访问(安全,且特别注意同步和异步)
// 创建数据库
    private CarMode(Context context){
        carDataBase = Room.databaseBuilder(context,CarDataBase.class,"car.db").
                allowMainThreadQueries().build();
        carDao = carDataBase.getCarDao();

    }
    // 获取单例
    public static CarMode getInstance(Context context){
        if(carMode == null){
            synchronized (CarMode.class){
                if(carMode == null)
                carMode = new CarMode(context);
            }
        }
        return carMode;
    }

RecyclerView的练习

  1. 先构建item再写Recycler View以及Adapt等等。
  2. RecyclerView中最关键的是其data数据,例如CarList这个对象。特别注意⚠️,数据的更新是直接针对这个数据进行更新的,而不是替换一个新的对象
Q1

点击相应的按钮recycler View没有进行刷新,但是数据库是发生了变化。当我关闭app后在recycler View是展现了更新后的数据。

这个主要notify等等相关方法的应用。这个我回家后进行修改了代码,相应的一些代码需要进行notify更新即可。

Dialog的自定义

可以参考文章进行学习巩固,基本的流程就是先构建一个视图然后再给他添加上相应的业务逻辑。
页面视图:还有相应的style设计等等

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    >
    <EditText
        android:id="@+id/edit_update_brand"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingLeft="10dp"
        android:inputType="text"
        android:background="@drawable/bg_edit"/>
    <EditText
        android:id="@+id/edit_update_price"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingLeft="10dp"
        android:inputType="number"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_edit"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_update"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/bg_btn"
            android:text="更新"/>
        <Button
            android:id="@+id/btn_quit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="10dp"
            android:background="@drawable/bg_btn"
            android:text="退出"/>
    </LinearLayout>
</LinearLayout>

这个和toast一样千万不要忘记写个show要不都不会展示出来的。

android -数据库储存Room(car info database)简易开发流程记录_android


android -数据库储存Room(car info database)简易开发流程记录_database_02