Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 在这里如何使用lambda表达式?_Kotlin_Lambda_Android Volley - Fatal编程技术网

Kotlin 在这里如何使用lambda表达式?

Kotlin 在这里如何使用lambda表达式?,kotlin,lambda,android-volley,Kotlin,Lambda,Android Volley,我有我的自定义截击课 类GetVolleyprivate val mContext:Context,private val url:String,private val onvolleySecsuccess:onvolleySecsuccess?,private val monvolleError:onvolleError?,private val mOnVolleyEnd:OnVolleyEnd?{ 私人娱乐截击{ Log.dGetVolleyUrl,url val request=JsonO

我有我的自定义截击课

类GetVolleyprivate val mContext:Context,private val url:String,private val onvolleySecsuccess:onvolleySecsuccess?,private val monvolleError:onvolleError?,private val mOnVolleyEnd:OnVolleyEnd?{ 私人娱乐截击{ Log.dGetVolleyUrl,url val request=JsonObjectRequestRequest.Method.GET,url,null,Response.Listener{Response:JSONObject-> Log.dGetVolleyResult,response.toString 如果response.hasresult&&response.getBooleanresult{ onVolleySuccess?.onSuccessresponse }else if response.hasmessage{ 如果mOnVolleyError!=null mOnVolleyError.OneErrorResponse.getStringmessage else Toast.makeTextMcContext、response.getStringmessage、Toast.LENGTH\u LONG.show } mOnVolleyEnd?.onEnd },Response.ErrorListener{volleyError:volleyError-> Log.dGetVolleyError,volleyError.message.toString val errorMessage:String=如果截击错误是NetworkError | |截击错误是AuthFailureError{ 没有互联网… }否则{ 未定义错误 } 如果mOnVolleyError!=null mOnVolleyError.OneErrorMessage else Toast.makeTextmContext、errorMessage、Toast.LENGTH\u LONG.show mOnVolleyEnd?.onEnd } Volley.newRequestQueuemContext.addrequest } 初始化{ 截击 } } 不同文件中的接口:

interface OnVolleySuccess {
    fun onSuccess(response: JSONObject)
}
______________________________________
interface OnVolleyError {
    fun onError(error: String)
}
______________________________________
interface OnVolleyEnd {
    fun onEnd()
}
当我使用自定义GetVolley请求时。我的代码如下所示:

GetVolley(this, url, object : OnVolleySuccess {
    override fun onSuccess(response: JSONObject) {
        parseResponse(response)
    }
}, object : OnVolleyError {
    override fun onError(error: String) {
        showError(error)
    }
}, null)
我希望它看起来像这样:

GetVolley(this, url, response -> {
    parseResponse(response)
}, error -> {
    showError(error)
}, null)
我的所有回调都可以为null,因此我可以在VolleySucture、onVolleyError和onVolleyEnd上设置null。

注意:如果需要,请使用Nicolas解决方案,因为如果不想在Java中使用这些类,它本机使用lambda

SAM转换,即使用lambda进行回调转换的接口,仅在Kotlin 1.4之后可用,该版本在EAP早期访问程序中

如果您拥有几个月前发布的接口,您可以使用一个有趣的关键字声明您的接口,该关键字将使其可用作lambda

截击成功的有趣界面{ 成功响应的乐趣:JSONObject } 截击错误的有趣接口{ 错误:字符串 } 截击端有趣的界面{ 有趣的 } 现在您可以这样使用它:

GetVolley(this, url, response -> {
    parseResponse(response)
}, error -> {
    showError(error)
}, null)
GetVolleythis,url,{response-> 解析响应响应 },{错误-> 淋浴误差 },空 //或者干脆 GetVolleythis,url,{parseResponseit},{SwarthRorit},null 如果您在采用Kotlin 1.4时遇到困难,请注意以下问题:

// add/change these in build.gradle:
plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.4-M1'
    // ...
}

repositories {
    // ...
    maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}

// add these in settings.gradle
pluginManagement {
    repositories {
        mavenCentral()
        maven { "https://dl.bintray.com/kotlin/kotlin-eap" }
        maven { "https://plugins.gradle.org/m2/" }
    }
}
注意:如果您愿意,可以使用Nicolas解决方案,因为如果您不想在Java中使用这些类,它本机使用lambda

SAM转换,即使用lambda进行回调转换的接口,仅在Kotlin 1.4之后可用,该版本在EAP早期访问程序中

如果您拥有几个月前发布的接口,您可以使用一个有趣的关键字声明您的接口,该关键字将使其可用作lambda

截击成功的有趣界面{ 成功响应的乐趣:JSONObject } 截击错误的有趣接口{ 错误:字符串 } 截击端有趣的界面{ 有趣的 } 现在您可以这样使用它:

GetVolley(this, url, response -> {
    parseResponse(response)
}, error -> {
    showError(error)
}, null)
GetVolleythis,url,{response-> 解析响应响应 },{错误-> 淋浴误差 },空 //或者干脆 GetVolleythis,url,{parseResponseit},{SwarthRorit},null 如果您在采用Kotlin 1.4时遇到困难,请注意以下问题:

// add/change these in build.gradle:
plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.4-M1'
    // ...
}

repositories {
    // ...
    maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}

// add these in settings.gradle
pluginManagement {
    repositories {
        mavenCentral()
        maven { "https://dl.bintray.com/kotlin/kotlin-eap" }
        maven { "https://plugins.gradle.org/m2/" }
    }
}
你所知道的就是所谓的单一抽象方法。它仅对Java接口可用,但将从Kotlin 1.4对Kotlin接口可用。看见现在,如果需要lambdas,请使用lambdas:

class GetVolley(
    private val mContext: Context, 
    private val url: String, 
    private val onVolleySuccess: ((response: JSONObject) -> Unit)?, 
    private val mOnVolleyError: ((error: String) -> Unit)?, 
    private val mOnVolleyEnd: (() -> Unit)?
) {
而不是做:

onVolleySuccess?.onSuccess(response)
你可以:

onVolleySuccess?.invoke(response)
你所知道的就是所谓的单一抽象方法。它仅对Java接口可用,但将从Kotlin 1.4对Kotlin接口可用。看见现在,如果需要lambdas,请使用lambdas:

class GetVolley(
    private val mContext: Context, 
    private val url: String, 
    private val onVolleySuccess: ((response: JSONObject) -> Unit)?, 
    private val mOnVolleyError: ((error: String) -> Unit)?, 
    private val mOnVolleyEnd: (() -> Unit)?
) {
而不是做:

onVolleySuccess?.onSuccess(response)
你可以:

onVolleySuccess?.invoke(response)

版本1.4尚未发布,因此我可以在生产中使用此版本吗?或者我应该等待它的发布?你可以使用它来查看@AnimeshSahu答案,但我强烈建议你改用lambdas,它更地道。此外,1.4还不稳定。@ShuhratjonJumaev EAP尚未准备好生产。通过参与EAP,您明确承认EAP版本可能不可靠,可能无法按预期工作,并且可能包含错误。@Nicolas我使用了您的解决方案。它在没有额外接口的情况下工作,我的代码看起来更好,这是我喜欢的。这并不重要,顺便说一句,我的代码现在看起来是这样的:``GetVolleythis,url,{//It:JSONObject parseResponseit},{//It:JSONObject-BashrorIt},null``它可以替换为response吗?是的,它只是默认名称。你可以写{response->来命名它。1.4版还没有发布,所以我可以在productio中使用这个版本吗
N或者我应该等待它的发布?你可以使用它来查看@AnimeshSahu答案,但我强烈建议你改用lambdas,它更地道。此外,1.4还不稳定。@ShuhratjonJumaev EAP尚未准备好生产。通过参与EAP,您明确承认EAP版本可能不可靠,可能无法按预期工作,并且可能包含错误。@Nicolas我使用了您的解决方案。它在没有额外接口的情况下工作,我的代码看起来更好,这是我喜欢的。这并不重要,顺便说一句,我的代码现在看起来是这样的:``GetVolleythis,url,{//It:JSONObject parseResponseit},{//It:JSONObject-BashrorIt},null``它可以替换为response吗?是的,它只是默认名称。您可以编写{response->来命名它。