[英]How to access the elements of the LiveData list "Livedata<List<Model>> in Kotlin?
In Java we access the first element of Livedata<List<Book>>
books using books[0]
, how to do that in Kotlin? 在Java中,我们使用
books[0]
访问Livedata<List<Book>>
图书的第一个元素,如何在Kotlin中做到这一点?
I have already tried everything. 我已经尝试了一切。
//Java code books[0];
// Java代码
books[0];
//Kotlin // books[0]
throws error //科特林//
books[0]
引发错误
Unresolved reference. 未解决的参考。 None of the following candidates is applicable because of receiver type mismatch: @InlineOnly public inline operator fun <@OnlyInputTypes K, V> Map.get(key: Int): Category?
由于接收者类型不匹配,以下候选者都不适用:@InlineOnly公共内联运算符fun <@OnlyInputTypes K,V> Map.get(key:Int):Category? defined in kotlin.collections
在kotlin.collections中定义
You could try books.get(0)
您可以尝试
books.get(0)
Kotlin allows you to some operators on types, one is infix, such as the +
as it is between to expresses/values. Kotlin允许您对类型进行某些运算符,一种是中缀,例如表示表达式/值之间的
+
。 I think the error is telling you you cant access books
using array notation. 我认为该错误告诉您无法使用数组符号访问
books
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.