Home > Enterprise >  Can somebody explain this Android-Kotlin code? Please
Can somebody explain this Android-Kotlin code? Please

Time:09-30

i am new to Android development with Kotlin pogramming language, i am not able to understand this code below, what i am guessing is that an instance(scanResultAdapter) is created from ScanResultAdapter class, this class has the code for recyclerView adapter. This code is in MainActivity.kt file in punchThrough's article about BLE -

    ScanResultAdapter(scanResults) { result ->
        // User tapped on a scan result
        if (isScanning) {
            stopBleScan()
        }
        with(result.device) {
            Log.w("ScanResultAdapter", "Connecting to $address")
            connectGatt(context, false, gattCallback)
        }
    }
}

CodePudding user response:

Which point you don't understand?

Regards your guess(the logic of code), that's right, I think.

It seems that you are not familiar with Kotlin, you can check them one by one. For an instance, I don't know this style code:

ScanResultAdapter(scanResults) { result ->
  ....
}

{ result -> .. }, I need to know that's the Lambdas Expressions Here's a reference

Maybe I don't understand the with of Kotlin, refer to this document and so on.

CodePudding user response:

You are right. (scanResults) is the first argument of constructor. Second is callback of click listener in curly brackets. When user tap on a device in the list this code in braces {} will be executed. Kotlin can take out of parentheses () arguments if it is functions.

  • Related