不能将 == 与 livedata<int> 和 int 一起使用,我还能如何检查?
Cannot use == with livedata<int> and int, how else can i check?
好的,所以我有一个 android 应用程序,并跟随 android 的房间查看教程。我已经设法让它与我的 recyclerview 一起按预期工作,以显示所有玩过的游戏的历史记录。我现在正在制作一个成就样式页面,想查看已达到的具体分数。
在我的 DAO 文件中有以下内容;
@Query("SELECT COUNT(*) from SavedScores WHERE difficulty = :Difficulty AND questioncount = :QuestionCount AND answeredcorrectly =:QuestionCount")
fun CheckRecordsForTrophy(Difficulty: String,QuestionCount:Int):Flow<Int>
然后在我的房间存储库中有这个;
val easy5: Flow<Int> = savedScoresDao.CheckRecordsForTrophy("Easy",5)
在我的视图模型中;
val easy5: LiveData<Int> = repository.easy5.asLiveData()
然后在 activity 我有以下内容;
在 oncreate 方法之前:
private val savedScoresViewModel: SavedScoresViewModel by viewModels {
SavedScoresViewModelFactory((application as ScoreApplication).repository)
}
在 oncreate 方法中:
var easy5var = savedScoresViewModel.easy5
savedScoresViewModel.easy5.observe(this) {
if(easy5var==0){}
}
我不是 100% 确定我是否应该按照我所做的所有这些步骤将所有数据放入我的回收器视图,但我已经有效地遵循了相同的步骤,但适配器等除外,因为我只是想了解如果他们达到给定成就的标准。
我目前在我的回购协议中有一个硬编码元素用于函数,即 CheckRecordsForTrophy("Easy",5) 我将在 activity 稍后计算如何设置
我似乎面临的问题是:
if(easy5var==0){}
我得到的错误是 Operator '==' cannot be applied to 'LiveData' and 'Int'.
目标:检查在我的分数table中是否有分数等于问题数量的记录,如果是,我将完成一项成就。我读过在查询 returns 中使用 count* 找到的记录数,所以我可以用它来计算他们是否应该获得成就。也就是说,没有记录,就没有成就。
您正在将 Int
与 LiveData
进行比较,而您可能想将 int 与实时数据的发射值进行比较。
var easy5var = savedScoresViewModel.easy5
savedScoresViewModel.easy5.observe(this) { newValue -> // this is what you've missed
// if(easy5var==0) {} <-- you've made the wrong equality check here
if (newValue == 0) {} // <-- probably this is what you've meant to do.
}
好的,所以我有一个 android 应用程序,并跟随 android 的房间查看教程。我已经设法让它与我的 recyclerview 一起按预期工作,以显示所有玩过的游戏的历史记录。我现在正在制作一个成就样式页面,想查看已达到的具体分数。
在我的 DAO 文件中有以下内容;
@Query("SELECT COUNT(*) from SavedScores WHERE difficulty = :Difficulty AND questioncount = :QuestionCount AND answeredcorrectly =:QuestionCount")
fun CheckRecordsForTrophy(Difficulty: String,QuestionCount:Int):Flow<Int>
然后在我的房间存储库中有这个;
val easy5: Flow<Int> = savedScoresDao.CheckRecordsForTrophy("Easy",5)
在我的视图模型中;
val easy5: LiveData<Int> = repository.easy5.asLiveData()
然后在 activity 我有以下内容;
在 oncreate 方法之前:
private val savedScoresViewModel: SavedScoresViewModel by viewModels {
SavedScoresViewModelFactory((application as ScoreApplication).repository)
}
在 oncreate 方法中:
var easy5var = savedScoresViewModel.easy5
savedScoresViewModel.easy5.observe(this) {
if(easy5var==0){}
}
我不是 100% 确定我是否应该按照我所做的所有这些步骤将所有数据放入我的回收器视图,但我已经有效地遵循了相同的步骤,但适配器等除外,因为我只是想了解如果他们达到给定成就的标准。
我目前在我的回购协议中有一个硬编码元素用于函数,即 CheckRecordsForTrophy("Easy",5) 我将在 activity 稍后计算如何设置
我似乎面临的问题是:
if(easy5var==0){}
我得到的错误是 Operator '==' cannot be applied to 'LiveData' and 'Int'.
目标:检查在我的分数table中是否有分数等于问题数量的记录,如果是,我将完成一项成就。我读过在查询 returns 中使用 count* 找到的记录数,所以我可以用它来计算他们是否应该获得成就。也就是说,没有记录,就没有成就。
您正在将 Int
与 LiveData
进行比较,而您可能想将 int 与实时数据的发射值进行比较。
var easy5var = savedScoresViewModel.easy5
savedScoresViewModel.easy5.observe(this) { newValue -> // this is what you've missed
// if(easy5var==0) {} <-- you've made the wrong equality check here
if (newValue == 0) {} // <-- probably this is what you've meant to do.
}