android - 从kotlin中的数据类中获取参数的值
问题描述
所以,我对 kotlin 还很陌生,还在学习东西,我有一个名为 Country 的数据类,有 4 个参数
县(名称:字符串,警察电话:字符串,救护车电话:字符串,消防队电话:字符串)
,其中包含 27 个对象的 listOf Country 和从 MainActivity 获取的 var nameC1。我已经调用了 forEach 的列表方法,我想用变量 nameC 来处理列表中的每个名称,并在找到匹配项时执行一些代码。
data class Country(val name: String, val police:String, val ambulance:String,val firefighter:String) {
}
var nameC1 = (activity as MainActivity).nameC
val numberList= listOf<Country>(
Country("Austria","133","144","122"),
Country("Belgium","101","100","100"),
Country("Bulgaria","166","150","160"),
Country("Croatia","192","194","193"),
Country("Cyprus","199","199","199"),
Country("Czech Republic","158","155","150"),
Country("Denmark","112","112","112"),
Country("Estonia","112","112","112"),
Country("Finland","112","112","112"),
Country("France","17","15","18"),
Country("Germany","110","112","112"),
Country("Greece","100","166","199"),
Country("Hungary","107","104","105"),
Country("Ireland","112","112","112"),
Country("Italy","113","118","115"),
Country("Latvia","112","112","112"),
Country("Lithuania","02","03","01"),
Country("Luxembourg","113","112","112"),
Country("Malta","112","112","112"),
Country("Netherlands","112","112","112"),
Country("Poland","997","999","998"),
Country("Portugal","112","112","112"),
Country("Romania","112","112","112"),
Country("Slovakia","158","155","150"),
Country("Slovenia","113","112","112"),
Country("Spain","092","061","080"),
Country("Sweden","112","112","112")
)
numberList.forEach { if (Country.name==nameC1 ) }
// i'm expecting String1==String2 but i'm
//stuck here because it says name is an unresolved reference
}
我会使用 getName() 但我知道在 kotlin 中 getter/setter 是自动化的(我不习惯)并且我在 kotlin doc 上没有发现任何有用的东西。网站,我在这个网站上看到有人建议实现 Kotlin-reflection,但我不明白默认情况下我不应该从类中获取参数。
解决方案
forEach
为集合中的每个元素创建一个 lambda。内部元素的默认名称lambda
是it
. 但是您也可以将其重命名为其他名称。参考文档
这是您的代码的工作示例
data class Country(val name: String, val police:String, val ambulance:String,val firefighter:String)
fun doThis(nameC1: String) {
val numberList= listOf<Country>(
Country("Austria","133","144","122"),
Country("Belgium","101","100","100"),
Country("Bulgaria","166","150","160"),
Country("Croatia","192","194","193"),
Country("Cyprus","199","199","199"),
Country("Czech Republic","158","155","150"),
Country("Denmark","112","112","112"),
Country("Estonia","112","112","112"),
Country("Finland","112","112","112"),
Country("France","17","15","18"),
Country("Germany","110","112","112"),
Country("Greece","100","166","199"),
Country("Hungary","107","104","105"),
Country("Ireland","112","112","112"),
Country("Italy","113","118","115"),
Country("Latvia","112","112","112"),
Country("Lithuania","02","03","01"),
Country("Luxembourg","113","112","112"),
Country("Malta","112","112","112"),
Country("Netherlands","112","112","112"),
Country("Poland","997","999","998"),
Country("Portugal","112","112","112"),
Country("Romania","112","112","112"),
Country("Slovakia","158","155","150"),
Country("Slovenia","113","112","112"),
Country("Spain","092","061","080"),
Country("Sweden","112","112","112") )
numberList.forEach {
if (it.name == nameC1) {
println("Match")
}
}
}
fun main() {
doThis("Slovenia")
}
在 play.kotlinlang.org 上亲自尝试 -链接
上述代码将println
在条件为 时执行该函数true
。
推荐阅读
- node.js - 标签它无法正常工作
- r - 如何在 SAS 9.4 中创建和使用 R 函数?
- selenium-chromedriver - 谁能告诉我如何点击我是一个更新鲜的链接,使用 selenium webdriver 测试以下 url
- angular - Angular 5 将 ElementRef 转换回组件
- java - 如何使用java查找格林威治标准时间的纪元格式当前时间
- database - 在这种情况下,如何在 ER 模型中转换 (0,N)(0,N)(0,N) 三元关系?
- python - 删除输入()之前插入的“返回空间” - Python 3.6
- microsoft-graph-api - Microsoft Graph API - 日历端点上的“使用资源进行身份验证时出错”400
- css - CSS使用选择器::after为文本添加小边框底部
- c# - 正则表达式作为字符串文字 C# .NET