当前位置:   article > 正文

Android jetpack room+ViewModel+liveData 数据自动更新_viewmodel更新数据

viewmodel更新数据

Android jetpack room的使用_安果移不动的博客-CSDN博客

接上篇博客

我们用google推荐的这种方式

 

修改:

StudentDao
  1. package com.anguomob.jecpack.dao
  2. import androidx.lifecycle.LiveData
  3. import androidx.room.*
  4. import com.anguomob.jecpack.bean.Student
  5. @Dao
  6. interface StudentDao {
  7. @Insert
  8. fun insertStudent(vararg student: Student)
  9. @Delete
  10. fun deleteStudent(vararg student: Student)
  11. @Query("DELETE FROM student")
  12. fun deleteAllStudent()
  13. @Update
  14. fun updateStudent(vararg student: Student)
  15. @Query("SELECT * FROM student")
  16. fun queryAllStudent(): LiveData<List<Student>>
  17. @Query("SELECT * FROM student where id = :id")
  18. fun getStudentById(id: Int): List<Student>
  19. }

 

新增 

StudentRepository类
  1. package com.anguomob.jecpack.repository
  2. import android.content.Context
  3. import android.os.AsyncTask
  4. import androidx.lifecycle.LiveData
  5. import com.anguomob.jecpack.bean.Student
  6. import com.anguomob.jecpack.dao.StudentDao
  7. import com.anguomob.jecpack.database.MyDataBase
  8. class StudentRepository(var context: Context) {
  9. private lateinit var studentDao: StudentDao
  10. init {
  11. val dataBase = MyDataBase.getSingle(context)
  12. this.studentDao = dataBase.getStudentDao();
  13. }
  14. fun insertStudent(vararg student: Student) {
  15. InsertStudentTask(studentDao).execute(*student)
  16. }
  17. fun deleteStudent(vararg student: Student) {
  18. DeleteStudentTask(studentDao).execute(*student)
  19. }
  20. fun updateStudent(vararg student: Student) {
  21. UpdateStudentTask(studentDao).execute(*student)
  22. }
  23. fun queryAllStudent():LiveData<List<Student>> {
  24. return studentDao.queryAllStudent()
  25. }
  26. fun deleteAllStudent() {
  27. DeleteAllStudentTask().execute()
  28. }
  29. inner class InsertStudentTask(var studentDao: StudentDao) : AsyncTask<Student, Any, Any>() {
  30. override fun doInBackground(vararg p0: Student) {
  31. studentDao.insertStudent(*p0)
  32. }
  33. }
  34. inner class UpdateStudentTask(var studentDao: StudentDao) : AsyncTask<Student, Any, Any>() {
  35. override fun doInBackground(vararg p0: Student) {
  36. studentDao.updateStudent(*p0)
  37. }
  38. }
  39. inner class DeleteStudentTask(var studentDao: StudentDao) : AsyncTask<Student, Any, Any>() {
  40. override fun doInBackground(vararg p0: Student) {
  41. studentDao.deleteStudent(*p0)
  42. }
  43. }
  44. inner class DeleteAllStudentTask() : AsyncTask<Any, Any, Any>() {
  45. override fun doInBackground(vararg p0: Any) {
  46. studentDao.deleteAllStudent()
  47. }
  48. }
  49. }

新增

StudentViewModel
  1. package com.anguomob.jecpack.viewmodel
  2. import android.app.Application
  3. import androidx.lifecycle.AndroidViewModel
  4. import androidx.lifecycle.LiveData
  5. import com.anguomob.jecpack.bean.Student
  6. import com.anguomob.jecpack.repository.StudentRepository
  7. class StudentViewModel(application: Application) : AndroidViewModel(application) {
  8. private lateinit var repository: StudentRepository
  9. init {
  10. repository = StudentRepository(application)
  11. }
  12. fun insertStudent(vararg student: Student) {
  13. repository.insertStudent(*student)
  14. }
  15. fun deleteStudent(vararg student: Student) {
  16. repository.deleteStudent(*student)
  17. }
  18. fun updateStudent(vararg student: Student) {
  19. repository.updateStudent(*student)
  20. }
  21. fun queryAllStudent(): LiveData<List<Student>> {
  22. return repository.queryAllStudent()
  23. }
  24. fun deleteAllStudent() {
  25. repository.deleteAllStudent()
  26. }
  27. }

修改页面代码

  1. package com.anguomob.jecpack.activity
  2. import android.annotation.SuppressLint
  3. import android.os.AsyncTask
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.view.View
  7. import androidx.lifecycle.ViewModelProvider
  8. import androidx.recyclerview.widget.LinearLayoutManager
  9. import com.anguomob.jecpack.R
  10. import com.anguomob.jecpack.adapter.RoomAdapter
  11. import com.anguomob.jecpack.bean.Student
  12. import com.anguomob.jecpack.dao.StudentDao
  13. import com.anguomob.jecpack.database.MyDataBase
  14. import com.anguomob.jecpack.databinding.ActivityRoomBinding
  15. import com.anguomob.jecpack.repository.StudentRepository
  16. import com.anguomob.jecpack.viewmodel.StudentViewModel
  17. class RoomActivity : AppCompatActivity() {
  18. private lateinit var binding: ActivityRoomBinding
  19. private lateinit var adapter: RoomAdapter
  20. private lateinit var viewModel: StudentViewModel
  21. @SuppressLint("NotifyDataSetChanged")
  22. override fun onCreate(savedInstanceState: Bundle?) {
  23. super.onCreate(savedInstanceState)
  24. binding = ActivityRoomBinding.inflate(layoutInflater)
  25. setContentView(binding.root)
  26. binding.rv.layoutManager = LinearLayoutManager(this)
  27. adapter = RoomAdapter()
  28. binding.rv.adapter = adapter;
  29. viewModel =
  30. ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(
  31. StudentViewModel::class.java
  32. );
  33. viewModel.queryAllStudent().observe(this) { data ->
  34. adapter.setData(data)
  35. adapter.notifyDataSetChanged();
  36. }
  37. }
  38. fun insert(view: View) {
  39. val s1 = Student("Jack", 20)
  40. val s2 = Student("Jack2", 21)
  41. viewModel.insertStudent(s1, s2)
  42. }
  43. fun change(view: View) {
  44. val s1 = Student(15, "LiuAn", 27)
  45. val s2 = Student(16, "LiuAN2", 18)
  46. viewModel.updateStudent(s1, s2)
  47. }
  48. fun delete(view: View) {
  49. val s1 = Student(20)
  50. viewModel.deleteStudent(s1)
  51. }
  52. fun clear(view: View) {
  53. viewModel.deleteAllStudent()
  54. }
  55. }

布局文件把查询替换为了清空

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".activity.RoomActivity">
  8. <androidx.constraintlayout.widget.Guideline
  9. android:id="@+id/guideline5"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:orientation="horizontal"
  13. app:layout_constraintGuide_percent="0.09165527" />
  14. <androidx.constraintlayout.widget.Guideline
  15. android:id="@+id/guideline7"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:orientation="horizontal"
  19. app:layout_constraintGuide_percent="0.19015048" />
  20. <androidx.constraintlayout.widget.Guideline
  21. android:id="@+id/guideline8"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:orientation="vertical"
  25. app:layout_constraintGuide_percent="0.5" />
  26. <Button
  27. android:id="@+id/button5"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:onClick="insert"
  31. android:text="增加"
  32. app:layout_constraintBottom_toTopOf="@+id/guideline5"
  33. app:layout_constraintEnd_toStartOf="@+id/guideline8"
  34. app:layout_constraintStart_toStartOf="parent"
  35. app:layout_constraintTop_toTopOf="parent" />
  36. <Button
  37. android:id="@+id/button6"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:onClick="change"
  41. android:text="修改"
  42. app:layout_constraintBottom_toTopOf="@+id/guideline5"
  43. app:layout_constraintEnd_toEndOf="parent"
  44. app:layout_constraintStart_toStartOf="@+id/guideline8"
  45. app:layout_constraintTop_toTopOf="parent" />
  46. <Button
  47. android:id="@+id/button7"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:onClick="delete"
  51. android:text="删除"
  52. app:layout_constraintBottom_toTopOf="@+id/guideline7"
  53. app:layout_constraintEnd_toStartOf="@+id/guideline8"
  54. app:layout_constraintStart_toStartOf="parent"
  55. app:layout_constraintTop_toTopOf="@+id/guideline5" />
  56. <Button
  57. android:id="@+id/button8"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:onClick="clear"
  61. android:text="清空"
  62. app:layout_constraintBottom_toTopOf="@+id/guideline7"
  63. app:layout_constraintEnd_toEndOf="parent"
  64. app:layout_constraintStart_toStartOf="@+id/guideline8"
  65. app:layout_constraintTop_toTopOf="@+id/guideline5" />
  66. <androidx.recyclerview.widget.RecyclerView
  67. android:id="@+id/rv"
  68. android:layout_width="0dp"
  69. android:layout_height="0dp"
  70. app:layout_constraintBottom_toBottomOf="parent"
  71. app:layout_constraintEnd_toEndOf="parent"
  72. app:layout_constraintStart_toStartOf="parent"
  73. app:layout_constraintTop_toTopOf="@+id/guideline7" />
  74. </androidx.constraintlayout.widget.ConstraintLayout>

 这种做法可以让数据自己展示出来。从而达到更加方便不用每次操作后手动去查询

非常的时尚

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/477722
推荐阅读
相关标签
  

闽ICP备14008679号