jetpac--初级mvvm结合Room

本文代码 在jetpac–Room文章中代码基础上修改。
一、Mvvm架构图
在这里插入图片描述
本问结合jetpack中 liveData + Room + ViewModel组件实现简单的mvvm架构。由于使用的是数据库,所以只能实现上图的左半部分。
LiveData : 根据lifecycle 对生命周期得检测,对在前台界面(onStart onResume)得数据进行更新,否则不更新。
ViewModel:(与mvvm中vm不是一个东西)保证数据的稳定性。activity 重建(例如横竖屏切换) ,数据不会丢失。只要activity没有ondestroy,数据就不会丢失。
Room:是jetpack的组件之一,是一个独立的数据库 ,是对sqlite(是用sql语句 c 语言实现,使用比较复杂)数据库的封装,使用比较简单。

本文主要对自己学习的一个总结,方便以后查阅复习,参考其他文章,尽量会标注对应链接。

二、代码实现(java)
按照上面的流程图从下到上依次实现:Room(Model) ->Reposititory仓库->ViewModel->view

步骤一:(Room 表)准备一张数据表(与jetpac–Room文章中使用的数据表Student相同)

package com.test.databindingtestjava.room.entity;

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

/**
 * @ClassName Student
 * @Description TODO
 * @Author lili
 * @DATE 2022/11/5 14:19
 */
@Entity
public class Student {
    @PrimaryKey(autoGenerate = true) // PrimaryKey 主键;autoGenerate自增长
    private int uid;

    //下面定义表中包含的列(字段)
    @ColumnInfo(name="name") // ColumnInfo 列信息
    private String name;

    @ColumnInfo(name="age")
    private int age;

    @ColumnInfo(name="address")
    private String address;

    @ColumnInfo(name="number")
    private String number;

    @ColumnInfo(name="grade")
    private int grade;

    //构造函数

    public Student() {}

    public Student(int uid,String name, int age, String address, String number,int grade) {
        this.uid = uid;
        this.name = name;
        this.age = age;
        this.address = address;
        this.number = number;
        this.grade = grade;
    }

    public Student(String name, int age, String address, String number,int grade) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.number = number;
        this.grade = grade;
    }

    //外部调用方法

    public void setUid(int uid) {
        this.uid = uid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getUid() {
        return uid;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getAddress() {
        return address;
    }

    public String getNumber() {
        return number;
    }

    public int getGrade() {
        return grade;
    }

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

步骤2 :(Room Dao)修改接口StudentDao,使数据表被LiveData包裹
修改getAll()方法

//查(所有)(原来)
    @Query("select *  from  Student")
    List<Student> getAll();

//查(所有)(修改后)
    @Query("select *  from  Student order by uid")
    LiveData<List<Student>> getAll();

步骤3:(Room basedata)准备数据库
与jetpac–Room中的数据库一致,省略

步骤4:(Repository)添加Repository仓库

package com.test.databindingtestjava.room.repository;

import android.content.Context;

import androidx.lifecycle.LiveData;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.test.databindingtestjava.room.dao.StudentDao;
import com.test.databindingtestjava.room.db.TestDB;
import com.test.databindingtestjava.room.entity.Student;

import java.util.List;

/**
 * @ClassName StudentRepository
 * @Description TODO
 * @Author lily
 * @DATE 2022/11/6 23:06
 */
public class StudentRepository {

    // 要操作的数据
    private LiveData<List<Student>> mLiveDataAllStudent;

    // 用户操作,只需要面向Dao
    private StudentDao mStudentDao;

    //数据库
    private TestDB mTestDB;

    // 创建构造函数,获取数据及对象初始化
    public StudentRepository(Context context) {

        mTestDB = TestDB.getTestDB(context);

        mStudentDao = mTestDB.getDao();

        if (mLiveDataAllStudent == null) {
            mLiveDataAllStudent = mStudentDao.getAll();
        }
    }

    // 因为用户接口调用是从上到下,一层一层调用,所以该类中 需要 与Dao中相同的接口方法,以提供API给ViewModel使用

    // 增
    public void insert(Student...students) {
        mStudentDao.insert(students);
    }

    // 删
    public void delete(Student student){
        mStudentDao.delete(student);
    }

    // 改
    public void update(Student student){
        mStudentDao.update(student);
    }

    //查(按姓名name查)
    public Student getByName(String name) {
        return mStudentDao.getByName(name);
    }

    //查(按主键id查)
    public List<Student> getById(int[] ids) {
        return mStudentDao.getById(ids);
    }

    //查(所有)
    public LiveData<List<Student>> getAll() {
        return mStudentDao.getAll();
    }
}

步骤4:(viewModel)实现ViewModel保证数据的稳定性

package com.test.databindingtestjava.room.viewmodel;

import android.app.Application;

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

import com.test.databindingtestjava.room.entity.Student;
import com.test.databindingtestjava.room.repository.StudentRepository;

import java.util.List;

/**
 * @ClassName StudentViewModel
 * @Description TODO
 * @Author lili
 * @DATE 2022/11/6 23:27
 */

// 继承AndroidViewModel,带有Application环境
public class StudentViewModel extends AndroidViewModel {

    // 根据调用流程,ViewModel需要调用仓库的接口方法,因此定义一个仓库对象
    private StudentRepository mStudentRepository;

    public StudentViewModel(@NonNull Application application) {
        super(application);
        if (mStudentRepository == null) {
            mStudentRepository = new StudentRepository(application);
        }
    }

   // 因为用户接口调用是从上到下,一层一层调用,所以该类中 需要 与仓库中相同的接口方法,以提供API给用户使用
   // 增
   public void insert(Student...students) {
       mStudentRepository.insert(students);
   }

    // 删
    public void delete(Student student){
        mStudentRepository.delete(student);
    }

    // 改
    public void update(Student student){
        mStudentRepository.update(student);
    }

    //查(按姓名name查)
    public Student getByName(String name) {
        return mStudentRepository.getByName(name);
    }

    //查(按主键id查)
    public List<Student> getById(int[] ids) {
        return mStudentRepository.getById(ids);
    }

    //查(所有)
    public LiveData<List<Student>> getAll() {
        return mStudentRepository.getAll();
    }
}

步骤5 :完成界面部分


// Activity.java
package com.test.databindingtestjava;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.test.databindingtestjava.room.dao.StudentDao;
import com.test.databindingtestjava.room.db.TestDB;
import com.test.databindingtestjava.room.entity.Student;
import com.test.databindingtestjava.room.view.StudentAdapter;
import com.test.databindingtestjava.room.viewmodel.StudentViewModel;

import java.util.List;

/**
 * @ClassName MainActivityTest
 * @Description TODO
 * @Author lili
 * @DATE 2022/11/2 13:46
 */
public class MainActivity extends AppCompatActivity {

    private ListView listView;
    // view只对ViewModel操作,定义Viewmodel的对象
    private StudentViewModel studentViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);

        studentViewModel = ViewModelProviders.of(this).get(StudentViewModel.class);

        // 眼睛  观察者
        studentViewModel.getAll().observe(this, new Observer<List<Student>>() {
            @Override
            public void onChanged(List<Student> students) {
                // 更新UI
                listView.setAdapter(new StudentAdapter(MainActivity.this, students));
            }
        });


        // 模拟 仓库
        new Thread()  {
            @Override
            public void run() {
                super.run();

                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 默认给数据库ROOM增加数据
                for (int i = 0; i < 50; i++) {
                    studentViewModel.insert(new Student("理查德", 35, "美国","13266785243",2));
                }
            }
        }.start();


        // 模拟仓库 数据库 数据被修改了,一旦数据库被修改,那么数据会驱动UI的发生改变
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    studentViewModel.update(new Student(4,"理查德" + i, 35, "美国","13266785243",2));
                }
            }
        }.start();
    }

}

// Adapter.java
package com.test.databindingtestjava.room.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.test.databindingtestjava.R;
import com.test.databindingtestjava.room.entity.Student;

import java.util.List;

/**
 * @ClassName StudentAdapter
 * @Description TODO
 * @Author lili
 * @DATE 2022/11/6 23:37
 */
public class StudentAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<Student> students;

    public StudentAdapter(Context context, List<Student> students) {
        inflater = LayoutInflater.from(context);
        this.students = students;
    }

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

    @Override
    public Object getItem(int position) {
        return students.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = inflater.inflate(R.layout.item, null);
        Student student = students.get(position);

        TextView tv_like = (TextView) view.findViewById(R.id.tv_id);
        tv_like.setText("number:"+student.getUid());

        TextView tv_style = (TextView) view.findViewById(R.id.tv_name);
        tv_style.setText("name:"+student.getName());

        return view;
    }
}

// activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:ignore="MissingConstraints" />

</LinearLayout>

// item.xml
<?xml version="1.0" encoding="UTF-8"?>

<!-- 配合 ListView用的 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666"
        android:textSize="28sp" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:textColor="#666"
        android:textSize="25sp"
        android:layout_marginLeft="30dp"/>

</LinearLayout>


三、最后总结一下
本文例子实现的功能 为数据驱动界面,即 当数据变化时,界面随之变化。LivaData包裹的数据表的数据,具有观察功能,当观察到数据有变化后,会调用adaper 更新界面UI。调用流程:view -> viewModel->仓库->数据库(通过Dao完成)。注意:该案例没有用databing组件,因此 没有数据绑定,仍然需要 调用adater更新数据。本案例中LiveData的功能就是 可以观察到 数据表的变化驱动UI更新。

用到的组件引用库:Room ViewModel

// 下面是 ROOM依赖相关的代码
    def room_version = "2.0.0-beta01"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version" // use kapt for Kotlin

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"
 
 //ViewModel
 implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值