当前位置: 首页 > news >正文

不花钱网站怎么做推广站长工具网站备案查询

不花钱网站怎么做推广,站长工具网站备案查询,来必力wordpress,在线生成logo图标免费文章目录 前言集成Room结合协程的使用总结 一、前言, 现在kotlin 是趋势,那必然就要用到协程,还有就是随着jetpack 的发力,带来了很多好用的库,比如今天提到Room,是一个类似greenDao的数据库。它不但支持kotlin协程…

文章目录

  • 前言
  • 集成Room
  • 结合协程的使用
  • 总结
一、前言,

现在kotlin 是趋势,那必然就要用到协程,还有就是随着jetpack 的发力,带来了很多好用的库,比如今天提到Room,是一个类似greenDao的数据库。它不但支持kotlin协程/RxJava,还具备编译期检查,是非常友好的库。我们一起来看下,在项目中怎么使用。

二、集成Room

1、创建一个kotlin项目,然后在app里面的build.gradle添加依赖:

plugins {
...id 'kotlin-android-extensions'id 'kotlin-kapt'
}
dependencies {
...//room数据库implementation "androidx.room:room-runtime:2.4.2"kapt "androidx.room:room-compiler:2.4.2" // Kotlin 使用 kaptimplementation "androidx.room:room-ktx:2.4.2"//Coroutines support for Room 协程操作库//lifecycleimplementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'}

此时,我们同步一下,开始运行项目。会报错:

Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8

解决:将项目的jdk1.8 改成 jdk11。
接下来还有报错:

Can't determine type for tag '<macro name="m3_comp_assist_chip_container_shape">?attr/shapeAppearanceCornerSmall</macro>'

原因是material 的版本高了的问题。

 implementation 'com.google.android.material:material:1.8.0'

解决:我们将material,改成1.6.0。项目就能正常运行了。

2、相关的类的创建

2.1创建数据库的实体类
通过在实体类上加注解@Entity,这样实体类就相当于是一张表

import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.android.parcel.Parcelize@Parcelize
@Entity(tableName = "Student")
data class Student(@PrimaryKeyvar id: String,var name: String
) : Parcelable

2.2创建实体类的Dao:
通过在Dao接口上加注解@Dao,就可以让dao轻松地完成增删改查。
另外可以将 suspend Kotlin 关键字添加到 DAO 方法中,用 Kotlin 协程功能,使这些方法成为异步方法。这样可确保不会在主线程上执行这些方法。

@Dao
interface StudentDao {//通过@Insert 注解的onConflict 解决冲突(如果有老的数据存在则会进行替换,如果没有就插入)@Insert(onConflict = OnConflictStrategy.REPLACE)suspend fun putStudent(cacheBean: Student)@Query("select * from Student where id =:id")suspend fun getStudent(id: String): Student?@Query("select * from Student")suspend fun getAllStudent(): List<Student>?@Deletesuspend fun delete(student: Student)@Update(onConflict = OnConflictStrategy.REPLACE)suspend fun update(student: Student)}

2.3 创建数据库的类
创建一个类继承RoomDatabase,加注解@Database,轻松地建数据库和建表

import android.util.Log
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase//后续的数据库升级通过这个version来控制,exportSchema是否导出数据库的配置信息
@Database(entities = [Student::class], version = 2, exportSchema = false)
abstract class StudentDatabase : RoomDatabase() {companion object {var dataBase: StudentDatabaseval TAG = StudentDatabase::class.java.simpleNameinit {//如果databaseBuilder改为inMemoryDatabaseBuilder则创建一个内存数据库(进程销毁后,数据丢失)dataBase = Room.databaseBuilder(MyApplication.getApplicationContext(), StudentDatabase::class.java, "db_user")//数据库的操作是否允许在主线程中执行.allowMainThreadQueries()//数据库创建和打开后的回调,可以重写其中的方法.addCallback(object : Callback() {override fun onCreate(db: SupportSQLiteDatabase) {super.onCreate(db)Log.d(TAG, "onCreate: db_student")}})//数据库升级异常之后的回滚.fallbackToDestructiveMigration().build()}}abstract fun getStudentDao(): StudentDao

2.4 创建一个MyApplication
通过它我们来获取context。

class MyApplication : Application() {init {instance = this}companion object {private var instance: MyApplication? = nullfun getApplicationContext() : Context {return instance!!.applicationContext}}override fun onCreate() {super.onCreate()}}
三、接下来就是使用了

通过点击按钮,进行增删改查的操作。
增加数据:

    btn_add.setOnClickListener {lifecycleScope.launch {val studentDao = StudentDatabase.dataBase.getStudentDao()studentDao.putStudent(Student("101","小李"));studentDao.putStudent(Student("102","小王"))}}

更改数据:

    btn_update.setOnClickListener {lifecycleScope.launch {val studentDao = StudentDatabase.dataBase.getStudentDao()val student = studentDao.update(Student("101","小陈"))Log.d(TAG,student.toString());}}

删除数据:

    btn_delete.setOnClickListener {lifecycleScope.launch {val studentDao = StudentDatabase.dataBase.getStudentDao()val student = studentDao.delete(Student("101","小陈"))Log.d(TAG,student.toString());val students = studentDao.getAllStudent()Log.d(TAG, "students: $students")}}

查询数据:

 lifecycleScope.launch {btn_query.setOnClickListener {lifecycleScope.launch {val studentDao = StudentDatabase.dataBase.getStudentDao()val students = studentDao.getAllStudent()Log.d(TAG, "students: $students")}}

备注:数据库的操作一定要放到子线程中,切不可在主线程中操作,虽然可以通过allowMainThreadQueries强制开启允许这么做,但这个是测试时用,实际项目中还是不要在主线程中操作数据库,避免遇到ANR问题

源码地址:https://github.com/shenshizhong/KotlinRoomDemo

总结

1 、导入依赖, 同步
2 、创建一个实体类Student,加注解@Entity就相当于一张表
3 、创建一个接口StudentDao,加注解@Dao就可以完成增删改查
4 、创建抽象类继承RoomDatabase,加注解@Database,轻松地建数据库和建表
5 、通过数据库的实例获取dao,调用方法

如果对你有一点点帮助,那是值得高兴的事情。:)
我的csdn:http://blog.csdn.net/shenshizhong
我的简书:http://www.jianshu.com/u/345daf0211ad

http://www.dt0577.cn/news/47621.html

相关文章:

  • 如何做网站搬家长尾关键词网站
  • 南漳网站设计温州高端网站建设
  • 如何做百度网站软文写作网站
  • 如何让wordpress主页不显示文章厦门seo厦门起梦
  • 沐众科技网站建设如何推广网页
  • 珠海做网站推广公司他达拉非的副作用和危害
  • 网上买保险网站中国互联网公司排名
  • 制作网站的公司哪家比较好产品推广网站哪个好
  • 网站seo方案设计谷歌搜索引擎入口手机版
  • 为某公司或企业做的门户网站如何用html制作一个网页
  • 做信息网站要办icp证吗好搜seo软件
  • 网站你的后台管理系统用什么做代运营电商公司
  • 羊肉口报关做网站福州seo关键字推广
  • 自己搭建一个网站需要多少钱?宁波seo推广联系方法
  • 代做预算网站怀柔网站整站优化公司
  • 布吉建网站福州网站建设团队
  • 17做网店网站网站没有友情链接
  • 网站语音转写怎么做seo怎么做优化计划
  • 易奇秀网站友好链接
  • 网站排名权重怎么做网站排名优化首页
  • 如何做适合手机访问的网站最全的搜索引擎
  • 京东门户网站怎么做下载百度app最新版
  • 如何计算网站pv网络营销顾问
  • 做网站图片大小app宣传推广方案
  • 东莞网站推广排行百度推销广告一年多少钱
  • 太原网站公司哪家好湖南seo优化首选
  • 网站改版会影响排名吗怎么优化网络
  • 企业网站流程b2b平台推广网站
  • 深圳极速网站建设报价aso优化方案
  • 做珠宝商城网站下载百度浏览器