【问题标题】:Android room java.util.NoSuchElementException: List is emptyAndroid机房java.util.NoSuchElementException:列表为空
【发布时间】:2018-01-31 09:21:39
【问题描述】:
这个查询有什么问题?我在编译时得到这个:
Error:Execution failed for task ':app:compileStagingJavaWithJavac'.
> java.util.NoSuchElementException: List is empty.
这就是我的 pojo 领域
【问题讨论】:
标签:
android
sqlite
android-sqlite
android-room
【解决方案1】:
好的,所以没有完美的答案,但我可以发布我的解决方案/walkaround。
- 您可以设置布尔值。
- 你可以得到布尔值。
但您不能使用布尔值(在 Maybe 或 Single 中)
解决方案:
- 在我的实体列中设置为布尔值
- 在我的 Dao 中,我使用布尔值设置值
-
当我想获得价值时,我就有了
Maybe<Integer> or Single<Integer>
就我而言,我使用Single<Integer>
以后做这样的事情(这只是一个例子!)
public Single<Boolean> getDatabaseValue(Integer id) {
return localDataSource
.getDatabaseValue(id)
.flatMap(this::toBooleanValue) // map to Single<Boolean>
.doOnError(throwable -> localDataSource.createFeed(id, false)) //no data, create entity
.onErrorResumeNext(Single.just(false)); //no data, return default value
}
private Single<Boolean>toBooleanValue(Integer databaseValue) {
return Single.just(databaseValue == 1);
}