【问题标题】:Android room database - Not sure how to convert a Cursor to this method's return typeAndroid 房间数据库 - 不确定如何将 Cursor 转换为此方法的返回类型
【发布时间】:2019-08-11 16:44:01
【问题描述】:

我正在尝试将此对象保存在 Room 数据库中,我阅读了有关 Typeconverters 的信息,用于将复杂对象转换为可以存储在数据库中的一个字段。我收到此错误: 错误:不确定如何将 Cursor 转换为此方法的返回类型 (androidx.lifecycle.LiveData>).---- 公共抽象 androidx.lifecycle.LiveData> queryQuestions(@org.jetbrains.annotations.NotNull() 我的代码是基于我发现的关于将对象转换为房间的类似问题的解决方案,但这对我不起作用。

我的问题课:

@Entity

    data class Question(@PrimaryKey var questionId: String = "",
                        val uid: String,
                        val name: String,
                        val photo: String,
                        val question: String,
                        val points: Int,
                        @ServerTimestamp val timestamp: Date? = null,
                        val options: ArrayList<Option>){
        constructor(): this("", "", "", "", "", 0, null, ArrayList())
    }

    data class Option(val optionText: String,
                      val correct: Boolean,
                      var votes: Int = 0,
                      var usersVoted: ArrayList<UserVoted> = ArrayList()){
        constructor(): this("", false /*,0, ArrayList()*/)
    }

    data class UserVoted(val name: String,
                         val photo: String){
        constructor(): this("", "")
    }

我的道课:

@Dao
interface QuestionDao {
    @Insert(onConflict = REPLACE)
    fun insertQuestions(question: ArrayList<Question>)

    @Query("SELECT * FROM question WHERE uid = :userId")
    fun queryQuestions(userId: String): LiveData<ArrayList<Question>>
}

我的数据库类:

@Database(entities = [Question::class], version = 1)
@TypeConverters(Converter::class)
abstract class AppDatabase : RoomDatabase(){
    abstract fun questionDao(): QuestionDao

}

我的转换器类:

class Converter {

    @TypeConverter
    fun fromTimestampToDate(value: Long?): Date? {
        return value?.let { Date(it) }
    }

    @TypeConverter
    fun fromDateToTimestamp(date: Date?): Long? {
        return date?.time?.toLong()
    }

    @TypeConverter
    fun fromStringToArrayList(value: String): ArrayList<Option> {
        Log.i("alengenije", "fromStringToArrayList string = $value")
        val listType = object:TypeToken<ArrayList<Option>>() {}.type
        return Gson().fromJson(value, listType)
    }

    @TypeConverter
    fun fromArrayLisrToString(list: ArrayList<Option>): String {
        val gson = Gson()
        return gson.toJson(list)
    }

}

【问题讨论】:

标签: android kotlin gson android-room


【解决方案1】:

ArrayList 更改为List

它对我有用。

【讨论】:

    【解决方案2】:

    您遇到的问题是使用 LiveData 引起的。 从 Android API 28 起,支持库已移至 androidx 这意味着您必须将所有内容移至 androidx。 您的问题在于 build.gradle 文件。 您可能在那里有 andoidx 生命周期,但 Room lib 是旧的 1.1.1 Room lib。 您应该像这样更新所有支持库以使用 androidx:

    def lifecycle_version = "2.0.0"
    
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    
    // Add RecyclerView dependency; must match SDK version
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    
    // Add FAB dependency
    implementation 'com.google.android.material:material:1.0.0-rc01'
    
    def room_version = "2.1.0-alpha07"
    
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 2019-12-12
      • 2022-06-15
      • 2018-03-08
      • 1970-01-01
      • 2021-12-30
      • 2020-12-01
      • 2019-12-03
      相关资源
      最近更新 更多