房间“不确定如何将光标转换为此方法的返回类型”
我是Android开发的新手,并且正在从事一个名为“水饮提醒”的项目。 这是我的DAO和数据库代码。 我会得到“ 房间不确定如何将光标转换为此方法的返回类型” 每次运行应用程序时。因此,我搜索了这一点,而我获得的是 ”
@Dao
interface Dao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertUser(user :User)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertDrinkData(drink: Drink)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertNotificationInfo(notification : Notification)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateUser(user: User)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateNotificationInfo(notification: Notification)
@Delete
suspend fun deleteSelectedDrinkData(drink : Drink)
@Query("SELECT *FROM User_table")
fun getAllUsers() : LiveData<List<User>>
@Query("SELECT * FROM $USER_TABLE_NAME LIMIT 1")
suspend fun readUserData() : User
@Query("SELECT * FROM $USER_TABLE_NAME LIMIT 1")
fun readData() : LiveData<User>
@Query("SELECT * FROM $USER_TABLE_NAME LIMIT 1")
suspend fun readNotificationData() : Notification
@Query("SELECT $COLUMN_DATE_DRINK , SUM($COLUMN_AMOUNT_DRINK) as Total GROUP BY $COLUMN_DATE_DRINK")
suspend fun readDrinkSumData() : MutableList<Sum>
@Query("SELECT $COLUMN_DATE_DRINK , SUM($COLUMN_AMOUNT_DRINK) as Total GROUP BY $COLUMN_DATE_DRINK")
fun readDrinkData() : LiveData<MutableList<Sum>>
@Query("SELECT * FROM $USER_TABLE_NAME WHERE $COLUMN_DATE_DRINK = :date")
suspend fun readDrinkDataSelectedDay(date:String): MutableList<Drink>
}
@Database(entities = [User::class,Drink::class,Notification::class],
version = DATABASE_VERSION)abstract class AppDataBase : RoomDatabase() {
abstract val daoInstance : Dao
companion object {
private var INSTANCE : AppDataBase? = null
fun getInstance(context : Context) : AppDataBase {
synchronized(this) {
var instance : AppDataBase? = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
AppDataBase::class.java,
"AppDataBase"
).build()
}
return instance
}
}
}}
class AppRepository(private val dao: Dao) {
fun readData() : LiveData<User> = dao.readData()
fun readDrinkData(): LiveData<MutableList<Sum>> = dao.readDrinkData()
suspend fun insertUser(user :User): Unit = dao.insertUser(user)
suspend fun insertDrinkData(drink: Drink): Unit = dao.insertDrinkData(drink)
suspend fun insertNotificationInfo(notification : Notification): Unit = dao.insertNotificationInfo(notification)
suspend fun updateUser(user: User) : Unit = dao.updateUser(user)
suspend fun updateNotificationInfo(notification: Notification): Unit = dao.updateNotificationInfo(notification)
suspend fun readUserData() : User = dao.readUserData()
suspend fun readNotificationData() : Notification = dao.readNotificationData()
suspend fun deleteSelectedDrinkData(drink : Drink): Unit = dao.deleteSelectedDrinkData(drink)
suspend fun readDrinkSumData(): MutableList<Sum>? = dao.readDrinkSumData()
suspend fun readDrinkDataSelectedDay(date: String): MutableList<Drink>? {
return dao.readDrinkDataSelectedDay(date)
}
这是我的房间版本:2.4.2
,这是错误: 在此处输入图像描述
I'm new to android development and I'm working on a project called " water drink reminder " .
this is my Dao and database codes.
i'm getting "Room Not sure how to convert a Cursor to this method's return type" error every time I run the app. so I searched about this and what I gained was " don't use LiveData with suspend keyword". I didn't use these two together but I still get this error. anyone can help?
@Dao
interface Dao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertUser(user :User)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertDrinkData(drink: Drink)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertNotificationInfo(notification : Notification)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateUser(user: User)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateNotificationInfo(notification: Notification)
@Delete
suspend fun deleteSelectedDrinkData(drink : Drink)
@Query("SELECT *FROM User_table")
fun getAllUsers() : LiveData<List<User>>
@Query("SELECT * FROM $USER_TABLE_NAME LIMIT 1")
suspend fun readUserData() : User
@Query("SELECT * FROM $USER_TABLE_NAME LIMIT 1")
fun readData() : LiveData<User>
@Query("SELECT * FROM $USER_TABLE_NAME LIMIT 1")
suspend fun readNotificationData() : Notification
@Query("SELECT $COLUMN_DATE_DRINK , SUM($COLUMN_AMOUNT_DRINK) as Total GROUP BY $COLUMN_DATE_DRINK")
suspend fun readDrinkSumData() : MutableList<Sum>
@Query("SELECT $COLUMN_DATE_DRINK , SUM($COLUMN_AMOUNT_DRINK) as Total GROUP BY $COLUMN_DATE_DRINK")
fun readDrinkData() : LiveData<MutableList<Sum>>
@Query("SELECT * FROM $USER_TABLE_NAME WHERE $COLUMN_DATE_DRINK = :date")
suspend fun readDrinkDataSelectedDay(date:String): MutableList<Drink>
}
@Database(entities = [User::class,Drink::class,Notification::class],
version = DATABASE_VERSION)abstract class AppDataBase : RoomDatabase() {
abstract val daoInstance : Dao
companion object {
private var INSTANCE : AppDataBase? = null
fun getInstance(context : Context) : AppDataBase {
synchronized(this) {
var instance : AppDataBase? = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
AppDataBase::class.java,
"AppDataBase"
).build()
}
return instance
}
}
}}
class AppRepository(private val dao: Dao) {
fun readData() : LiveData<User> = dao.readData()
fun readDrinkData(): LiveData<MutableList<Sum>> = dao.readDrinkData()
suspend fun insertUser(user :User): Unit = dao.insertUser(user)
suspend fun insertDrinkData(drink: Drink): Unit = dao.insertDrinkData(drink)
suspend fun insertNotificationInfo(notification : Notification): Unit = dao.insertNotificationInfo(notification)
suspend fun updateUser(user: User) : Unit = dao.updateUser(user)
suspend fun updateNotificationInfo(notification: Notification): Unit = dao.updateNotificationInfo(notification)
suspend fun readUserData() : User = dao.readUserData()
suspend fun readNotificationData() : Notification = dao.readNotificationData()
suspend fun deleteSelectedDrinkData(drink : Drink): Unit = dao.deleteSelectedDrinkData(drink)
suspend fun readDrinkSumData(): MutableList<Sum>? = dao.readDrinkSumData()
suspend fun readDrinkDataSelectedDay(date: String): MutableList<Drink>? {
return dao.readDrinkDataSelectedDay(date)
}
this is my room version : 2.4.2
and this is errors :
enter image description here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
livedata
作为返回类型。使用:
而不是:
如果您想在房间型号中使用复杂的对象,则必须为这些类型添加一个房间交换器。阅读以下内容:使用ROOM 。例如,您可以使用
gson
将它们转换为json-string
然后,您必须将转换器添加到数据库中:
Don't use
LiveData
as the return type.Use:
Instead of:
In case you want to use complex objects in your room models, You have to add a Room-Converter for those types. Read this: Referencing complex data using Room.
For example you can use
GSON
to convert them to ajson-string
Then you have to add your converters to your database:
您在代码中使用暂停关键字如下:
暂停Fun ReadUserData():user = dao.readuserdata()
因此,它引发了相关的错误。
我建议您不要在此层中使用Livedata。您可能很快就会面临其他问题。只需返回数据实体并在视图模型中更新实时数据即可。
You are using suspend keyword in code like following:
suspend fun readUserData() : User = dao.readUserData()
so it throws the related error.
I suggest you to not use LiveData in this layer. you will probably face other issues soon. just return the data entity and update live data in view model.