简体   繁体   English

Kotlin - 如何返回嵌套变量?

[英]Kotlin - How To Return Nested Variable?

I am new in Kotlin Language and don't know how to RETURN variable from the following function.我是 Kotlin 语言的新手,不知道如何从以下 function 中返回变量。
Can you please help?你能帮忙吗?

Here is the code...这是代码...

downloadData.setOnClickListener {

            val handler = Handler(Looper.getMainLooper())
            handler.post {
                val fetchData =
                    FetchData("http://localhost/xampp/CRM/PHP/show_contacts_db.php")

                if (fetchData.startFetch()) {
                    if (fetchData.onComplete()) {
                        val result = fetchData.data.toString()
                        
                        Log.i("FetchData", result)
                        

                        val companyName = result.substringAfter("Name: ").substringBefore(";")
                        showContactName.text = "${companyName}"
                        val companyNumber = result.substringAfter("Number: ").substringBefore(";")
                        showContactNumber.text = "${companyNumber}"
                    }
                }
            }
        }

companyName and companyNumber needed to be returned so I can use it in other places. companyNamecompanyNumber需要返回,以便我可以在其他地方使用它。

PS. PS。 When I Try to use Return companyNumber I have a message that "return" is not allowed here.当我尝试使用Return companyNumber时,我有一条消息,这里不允许“return”。

Generally with lambdas, you don't explicitly return a value - the lambda returns the value of the last expression.通常使用 lambda,您不会显式return值 - lambda 返回最后一个表达式的值。 Using your code as an example (it won't actually work but we'll get to that:):以您的代码为例(它实际上不起作用,但我们会解决的:):

handler.post {
    ...
    companyNumber
}

which is the same as how things like map calls take a transformation function这与map调用进行转换 function 之类的方法相同

listOf(1, 2, 3).map { it * 2 }

that's just doubling each number, but the result is being implicitly returned and stored in the resulting list, right?这只是将每个数字加倍,但结果被隐式返回并存储在结果列表中,对吗? And it lets you chain lambdas together, since each one evaluates to a value (which might be Unit ) if it "doesn't return a result"它允许您将 lambda 链接在一起,因为如果它“不返回结果”,每个都会计算一个值(可能是Unit

If you want, you can explicitly use what's called a qualified return :如果需要,您可以显式使用所谓的合格 return

handler.post {
    ...
    return@post companyNumber
}

where you're naming the function call you're returning to.您将要返回的 function 调用命名为。

Kotlin docs: returning a value from a lambda expression Kotlin 文档:从 lambda 表达式返回一个值


Also if you want to return two values, you can't do that - so you'll have to bundle them up in a single object.此外,如果你想返回两个值,你不能这样做——所以你必须将它们捆绑在一个 object 中。 You could just return a Pair , or create a data class that's more readable:您可以只返回一个Pair ,或者创建一个更具可读性的数据 class :

return@post Pair(companyName, companyNumber)

//or
data class CompanyDeets(val name: String, val number: String)
...
return@post CompanyDeets(companyName, companyNumber)

But aside from how you do it in general, why do you want to return anything here?但是除了你一般是怎么做的,你为什么要在这里退回任何东西? Handler#post takes a Runnable which returns nothing ( void in Java), and View.OnClickListener#onClick doesn't return anything either. Handler#post接受一个Runnable ,它什么都不返回(Java 中为void ),而View.OnClickListener#onClick也不返回任何东西。

Neither of them would do anything with a value you returned - and if you explicitly return a value, that means your lambda's signature doesn't match (right now it's implicitly returning Unit to match what's expected by the caller), and you'll get an error他们都不会对你返回的值做任何事情——如果你显式返回一个值,这意味着你的 lambda 签名不匹配(现在它隐式返回Unit以匹配调用者的预期),你会得到一个错误

What you probably want to do instead, is create a function inside your class ( Activity or whatever) that uses your data, something like fun doSomethingWith(companyName: String, companyNumber: String) and call that inside your lambda.您可能想要做的是在您的 class ( Activity或其他)中创建一个 function 使用您的数据,例如fun doSomethingWith(companyName: String, companyNumber: String)并在您的 Z945F3FC4495188A463B 中调用它That's way you're executing code in reaction to a click这就是您执行代码以响应点击的方式

just declare var Company Name in global, or create a function with that params只需在全局中声明 var Company Name,或使用该参数创建一个 function

var companyName: String? var companyName: 字符串? = null = null

handler.post {... companyName = result.substringAfter("Name: ").substringBefore(";") } handler.post {... companyName = result.substringAfter("Name:").substringBefore(";") }

OR或者

handler.post {... save(result.substringAfter("Name: ").substringBefore(";")) } handler.post {... save(result.substringAfter("Name: ").substringBefore(";")) }

fun save(companyName: String){... }有趣的保存(公司名称:字符串){...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM