![](/img/trans.png)
[英]How do I resolve red filenames with Android if I can't find any issues?
[英]ArrayList - I can't resolve this red line code
之前的代碼如下
class MainAdapter(val locationList: ArrayList<HashMap<String,String>>): RecyclerView.Adapter<MainAdapter.ViewHolder>() {
val byDates = locationList.groupBy { it["time"] } //this fine
}
之后我改為使用 model
class MainAdapter(val locationList: ArrayList<Model>): RecyclerView.Adapter<MainAdapter.ViewHolder>() {
val byDates = locationList.groupBy { it["time"] } //this red line i cant resolve
}
這是我的 model
data class Model(val name : String?,
val address : String?,
val time : String?)
這是因為在第一個(工作)示例中it
引用了HashMap<String, String>
的實例,因此調用it["time"]
實際上等於調用it.get("time")
。
然而,在第二個示例中, it
指的是 Model。 在 Kotlin 中,您無法通過“括號”語法訪問屬性(例如在 JS 中),因此會顯示錯誤。 正確的代碼是locationList.groupBy { it.time }
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.