Tag: 安卓室

如何使用Kotlin精简构造函数语法来使用Room库的@Ignore注解?

我试图使用android室库和Kotlin的紧凑语法指定一个构造函数与默认参数值。 像这样的东西: @Entity class MyEntity(var myString:String = “non-trivial string”) { @PrimaryKey(autoGenerate = true) var myIndex:Int = 0 } 但是我得到这个警告信息: 有多个好的构造函数,Room会选择无参数的构造函数。 您可以使用@Ignore注释来消除不需要的构造函数。 语法允许在这个紧凑的Kotlin风格的构造函数中写入Room的@Ignore注释吗? 我知道我可以做这样的事情来消除这个警告信息,但是它更冗长。 这也使得构造函数arg的默认值显得冗余/无益: @Entity class MyEntity() { @Ignore constructor(myString:String = “non-trivial string”) : this() { this.myString = myString } @PrimaryKey(autoGenerate = true) var myIndex:Int = 0 var myString:String? = null } 我怎样才能宣布一个房间的实体,但仍利用Kotlin的简洁性? 非常感谢你。

在使用Kotlin协同程序时,与Room dao类错误

我试图使用kotlin协同程序通过这里描述的方法访问房间数据库,添加了插件和依赖项,并启用了gradle中的kotlin协程。 在gradle文件中: kotlin { experimental { coroutines ‘enable’ } } dependencies { implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21” …} 所以我为dao类中的所有方法添加了suspend关键字,如下所示: 道class @Query(“select * from myevent”) suspend fun all(): List @Delete suspend fun deleteEvent(event: MyEvent) … 并建立,然后得到这些错误 错误 e: C:\Users\projectpath\app\build\tmp\kapt3\stubs\debug\com\robyn\myapp\data\source\local\EventsDao.java:39: error: Deletion methods must either return void or return int (the number of deleted rows). public abstract java.lang.Object deleteEventById(@org.jetbrains.annotations.NotNull() ^ […]

无法find填充字段 – 使用Kotlin与房间数据库

我正在与Room持久性库集成。 我在Kotlin有一个数据类: @Entity(tableName = “story”) data class Story ( @PrimaryKey val id: Long, val by: String, val descendants: Int, val score: Int, val time: Long, val title: String, val type: String, val url: String ) @Entity和@PrimaryKey注释用于Room库。 当我尝试构建时,错误是失败的: Error:Cannot find setter for field. Error:Execution failed for task ‘:app:compileDebugJavaWithJavac’. > Compilation failed; see the compiler error output […]

Kotlin使用Room persistence lib进行代理

我目前正在开发一个新的Android应用程序使用Kotlin。 我试图实现数据存储的空间,但我没有得到它与科特林代表合作。 我创建了一个Identifier委托,以确保id在初始化后不会被更改。 代表看起来像这样: class Identifier: ReadWriteProperty { private var currentValue = -1L override fun getValue(thisRef: Any?, property: KProperty): Long { if (currentValue == -1L) throw IllegalStateException(“${property.name} is not initialized.”) return currentValue } override fun setValue(thisRef: Any?, property KProperty, value: Long) { if (currentValue != -1L) throw IllegalStateException(“${property.name} can not be changed.”) currentValue = value […]

房间,KOTLIN问题只与果冻豆

我在Kotlin的应用程序中使用了Room库。 应用程序适用于Android 4.4及以上版本。 但与Android的果冻豆(测试4.1和4.2)有这个问题: Caused by: java.lang.RuntimeException: cannot find implementation for com.app.example.database.AppDatabase. AppDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:90) at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:440) at com.app.example.application.MyApplication.onCreate(MyApplication.kt:36) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999) 我试过使用 kapt { generateStubs = true } 和 apply plugin: ‘kotlin-kapt’ 没有不同。 同样的错误。

Android的房间+ Kotlin模式

Android的房间文件说,我们应该按照单例设计模式时,实例化一个AppDatabase对象。 我正在考虑这个问题,我想知道是否建议将AppDatabase类放入我的Application类中。 或者如果我可以使用Kotlin单例。 假设我有一个名为CarroDAO的DAO和一个RoomDatabase的CarrosDatabase类。 使用Kotlin对象/单例创建DatabaseManager类可以吗? object DatabaseManager { private var dbInstance: CarrosDatabase init { val appContext = MyApplication.getInstance().applicationContext dbInstance = Room.databaseBuilder( appContext, CarrosDatabase::class.java, “mybd.sqlite”) .build() } fun getCarroDAO(): CarroDAO { return dbInstance.carroDAO() } } 所以我可以像这样得到DAO类: val dao = DatabaseManager.getCarroDAO()