kotlin--Flow结合Room运用

本文介绍如何使用Kotlin结合Room实现MVVM架构的应用,包括添加依赖、创建数据库实体类、DAO接口、ViewModel及RecyclerView适配器,并展示了如何通过LiveData进行数据绑定。
摘要由CSDN通过智能技术生成
kotlin结合Room在实际项目中的运用
架构还是MVVM,DataBinding+ViewModel+LiveData的组合

效果:

1.首先添加Room依赖

添加kapt插件:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}
def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
2.创建数据库相关类

entity:

package com.aruba.flowapplyapplication.database.entity

import androidx.room.Entity
import androidx.room.PrimaryKey

/**
 * Created by aruba on 2021/9/20.
 */
@Entity
data class UserInfo(
    @PrimaryKey val id: Int,
    var userName: String,
    var age: Int
)

Dao,之前我们需要使用异步任务操作Dao,kotlin则可以使用挂起函数,标识使用协程操作:

package com.aruba.flowapplyapplication.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.aruba.flowapplyapplication.database.entity.UserInfo
import kotlinx.coroutines.flow.Flow

/**
 * Created by aruba on 2021/9/20.
 */
@Dao
interface UserInfoDao {
    //id重复的替换
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(userInfo: UserInfo)

    //返回Flow,由于Flow需要使用collect,该函数为挂起函数,所以不需要加suspend了
    @Query("SELECT * FROM userinfo")
    fun getUserInfoList(): Flow<List<UserInfo>>
}

Database:

package com.aruba.flowapplyapplication.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.aruba.flowapplyapplication.database.dao.UserInfoDao
import com.aruba.flowapplyapplication.database.entity.UserInfo

private const val DB_NAME: String = "my.db"

/**
 * Created by aruba on 2021/9/20.
 */
@Database(entities = [UserInfo::class], version = 1, exportSchema = true)
abstract class MyDatabase : RoomDatabase() {

    abstract fun getUserDao(): UserInfoDao

    companion object {
        private var instance: MyDatabase? = null

        fun getInstance(): MyDatabase {
            checkNotNull(instance) { "init has not been called" }
            return instance as MyDatabase
        }

        fun init(context: Context) {
            synchronized(this) {
                instance ?: Room.databaseBuilder(
                    context,
                    MyDatabase::class.java, DB_NAME
                ).build().let { instance = it }
            }
        }
    }
}
3.定义ViewModel

使用LiveData对三个EditText进行双向绑定

package com.aruba.flowapplyapplication.viewmodel

import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aruba.flowapplyapplication.database.MyDatabase
import com.aruba.flowapplyapplication.database.dao.UserInfoDao
import com.aruba.flowapplyapplication.database.entity.UserInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import androidx.databinding.ObservableField


/**
 * Created by aruba on 2021/9/20.
 */
class UserInfoViewModel : ViewModel() {
    val id = MutableLiveData<String>()
    val name = MutableLiveData<String>()
    val age = MutableLiveData<String>()

    private val userInfoDao: UserInfoDao by lazy {
        MyDatabase.getInstance().getUserDao()
    }

    fun insert(v: View) {
        if (id.value == null || name.value == null || age.value == null) {
            return
        }

        val userInfo = UserInfo(id.value!!.toInt(), name.value!!, age.value!!.toInt())
        viewModelScope.launch(Dispatchers.IO) {
            userInfoDao.insert(userInfo)
        }
    }

    fun getUserInfo(): Flow<List<UserInfo>> {
        return userInfoDao
            .getUserInfoList()
            .flowOn(Dispatchers.IO)
    }

}
4.定义RecyclerViewAdapter
package com.aruba.flowapplyapplication.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.aruba.flowapplyapplication.R
import com.aruba.flowapplyapplication.database.entity.UserInfo
import com.aruba.flowapplyapplication.databinding.ItemUserinfoBinding

/**
 * Created by aruba on 2021/9/20.
 */
class UserInfoAdapter() : RecyclerView.Adapter<UserInfoAdapter.MyViewHolder>() {
    private var data = ArrayList<UserInfo>()

    class MyViewHolder(val binding: ItemUserinfoBinding) : RecyclerView.ViewHolder(binding.root)

    fun setData(data: List<UserInfo>) {
        this.data.clear()
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val binding: ItemUserinfoBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_userinfo,
            parent, false
        )

        return MyViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.binding.userInfo = data[position]
    }

    override fun getItemCount(): Int {
        return data.size
    }
}
5.Fragment中实例化ViewModel,以及进行数据绑定等操作
package com.aruba.flowapplyapplication

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.aruba.flowapplyapplication.adapter.UserInfoAdapter
import com.aruba.flowapplyapplication.database.MyDatabase
import com.aruba.flowapplyapplication.databinding.FragmentRoomBinding
import com.aruba.flowapplyapplication.viewmodel.UserInfoViewModel
import kotlinx.coroutines.flow.collect

class RoomFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //初始化数据库
        MyDatabase.init(requireContext())
        
        val inflate = DataBindingUtil.inflate<FragmentRoomBinding>(
            layoutInflater,
            R.layout.fragment_room,
            container,
            false
        )

        //实例化ViewModel
        val userInfoViewModel = ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory(requireActivity().application)
        ).get(UserInfoViewModel::class.java)
        inflate.userInfoViewModel = userInfoViewModel
        inflate.recyclerview.adapter = UserInfoAdapter()
        inflate.recyclerview.layoutManager = LinearLayoutManager(context)
        inflate.lifecycleOwner = viewLifecycleOwner

        //开启协程对数据库的表进行监听
        lifecycleScope.launchWhenCreated {
            //每当UserInfo表发生变化,Flow都会把UserInfo列表发射出去,那么我们
            //在collect中就可以获取到
            userInfoViewModel.getUserInfo().collect {
                (inflate.recyclerview.adapter as UserInfoAdapter).setData(it)
            }
        }
        return inflate.root
    }

}
项目地址:https://gitee.com/aruba/flow-apply-application.git
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值