Kotlin Room:不确定如何将游标转换为此方法的返回类型
我无法在Dao中使用LiveData。删除LiveData可以解决编译错误。代码如下: 我已经查看了我的Gradle构建文件。我没有使用任何旧的支持库。我的代码也没有其他常规错误 基类:Kotlin Room:不确定如何将游标转换为此方法的返回类型,kotlin,android-room,Kotlin,Android Room,我无法在Dao中使用LiveData。删除LiveData可以解决编译错误。代码如下: 我已经查看了我的Gradle构建文件。我没有使用任何旧的支持库。我的代码也没有其他常规错误 基类: @Entity @JsonClass(generateAdapter = false) abstract class BaseEntity( @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "_id") var id: Long =
@Entity
@JsonClass(generateAdapter = false)
abstract class BaseEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
var id: Long = 0L,
@ColumnInfo(name = "_created_at")
@Json(name = "created_at")
var createdAt: Date = Date(),
@ColumnInfo(name = "_updated_at")
@Json(name = "updated_at")
var updatedAt: Date = Date(),
@ColumnInfo(name = "_created_by")
@Json(name = "created_by")
var createdBy: String = ""
) {
init {
val applicationContext = BaseApp.applicationContext() as BaseApp
val username = applicationContext.sharedPreference?.getString(
applicationContext.getString(R.string.setting_login_username),
""
)
if (username != null) {
createdBy = username
}
}
}
@Entity
@JsonClass(generateAdapter = true)
open class Queue(
@NonNull
@ColumnInfo(name = "name")
@Json(name = "name")
var name: String = "",
@NonNull
@ColumnInfo(name = "queue_type")
@Json(name = "queue_type")
var queueType: QueueType? = null,
@NonNull
@ColumnInfo(name = "queue_status")
@Json(name = "queue_status")
var queueStatus: QueueStatus? = null,
@ColumnInfo(name = "server_response")
@Json(name = "server_response")
var serverResponse: String = ""
) : BaseEntity()
@Dao
interface QueueDao {
@Query("SELECT * FROM Queue")
suspend fun getQueue(): LiveData<List<Queue>>
}
Complete gradle build file:
===========================
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "____"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.41'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02'
// Room for database
def room_version = '2.1.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:2.1.0"
// moshi json parser
implementation("com.squareup.moshi:moshi-kotlin:1.8.0")
implementation("com.squareup.moshi:moshi-adapters:1.8.0")
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.8.0")
// zxing-android-embedded
implementation('com.journeyapps:zxing-android-embedded:3.6.0'){transitive = false}
implementation 'com.google.zxing:core:3.3.3'
// kotlin coroutines and retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'
implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
// glide for image viewing/fetching
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
// dexter for permissions
implementation 'com.karumi:dexter:5.0.0'
// materialDrawer
implementation "com.mikepenz:materialdrawer:6.1.1"
// Google flexbox
implementation 'com.google.android:flexbox:1.1.0'
// UI Elements
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "com.google.android.material:material:1.0.0"
implementation "androidx.annotation:annotation:1.1.0"
// tests
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
只需从Dao中删除LiveData即可成功编译代码。Room不支持带有挂起函数的LiveData。另外,LiveData已经在后台线程上工作,应该直接使用,而不使用协同程序 从代码中删除挂起解决了错误
error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.List<data.basequeue.Queue>>).
public abstract java.lang.Object getQueue(@org.jetbrains.annotations.NotNull()