Android Room数据库-不确定如何将游标转换为此方法的返回类型

6

我正在尝试将这个对象保存到Room数据库中,我了解到可以使用TypeConverters将复杂对象转换为一个字段,从而可以存储在数据库中。但是我遇到了以下错误:

error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData>).---- public abstract androidx.lifecycle.LiveData> queryQuestions(@org.jetbrains.annotations.NotNull()

我的代码是基于我在解决与room对象转换的类似问题时找到的解决方案,但对我来说不起作用。

我的Question类:

@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类:

@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)
    }

}

你找到解决方案了吗? - Mohamed Nageh
可能是[Room“不确定如何将游标转换为此方法的返回类型”:哪种方法?](https://dev59.com/fFYO5IYBdhLWcg3wDNXI)的重复问题。 - Mehul Kanzariya
2个回答

2

ArrayList更改为List

这对我有效。


0
你遇到的问题是由于使用LiveData引起的。 从Android API 28开始,支持库已经迁移到了androidx。 这意味着你需要将所有东西都迁移到androidx。 你的问题在于一个build.gradle文件。 你很可能在那里有androidx lifecycle,但是Room库是旧版的1.1.1 Room库。 你应该更新所有支持库以使用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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接