【发布时间】:2022-01-21 02:08:43
【问题描述】:
我不知道如何从以下函数返回变量。
这里是代码...
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 和 companyNumber 需要返回,以便我可以在其他地方使用它。
当我尝试使用 Return companyNumber 时,我收到一条消息,这里不允许“返回”。
【问题讨论】:
-
看看at this example。一般来说,一个好的方法是将这些值传递给另一个处理它们而不是返回它们的函数,或者将它们设置为类成员。
标签: android android-studio kotlin return return-value