I'm trying to save this object in Room database, I read about Typeconverters for converting complex objects in one filed that can be stored in database. I am getting this error: 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() My code is based on solutions that I found for similar problems with converting objects for room but this isn't working for me.
My question class:
@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("", "")
}
My dao class:
@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>>
}
My database class:
@Database(entities = [Question::class], version = 1)
@TypeConverters(Converter::class)
abstract class AppDatabase : RoomDatabase(){
abstract fun questionDao(): QuestionDao
}
My converter class:
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)
}
}
Change ArrayList to List.
It works for me.
The problem that you have is caused by the use of LiveData. From Android API 28 the support libraries have moved to androidx This means that you have to move EVERYTHING to androidx. Your problem lies with a build.gradle file. You probably have the andoidx lifecycle there but Room lib as the old 1.1.1 Room lib. You should update all the support libraries to use androidx like so:
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"