How I can use callback in Kotlin?

后端 未结 7 2185
Happy的楠姐
Happy的楠姐 2020-12-13 04:05

I have View and one CircleShape , which should show toast in this View. And I use it in main Activity. This is my interface

interface OnClickListenerInterfa         


        
7条回答
  •  别那么骄傲
    2020-12-13 04:14

    to use Kotlin callbacks , I use them in my api calls for success or failure use

    create enum class for state

    enum class APIState(val result: Boolean) {
    SUCCESS(true),
    FAILURE(false)}
    

    use call back in fun

     private fun fetchFeesList(studentID:String,call:(APIState)->Unit){
     ... do stuff here , and use call(APIState.SUCCESS) or call(APIState.FAILURE) }
    

    when calling function fetchFeesList , call it like

    fetchFeesList(studentID){
            val res = it.result
            if(res){
                toast("success")
            }else {
                toast("failure")
            }
        }
    

    for toast("message") , use Anko Lib from GitHub : - https://github.com/Kotlin/anko

提交回复
热议问题