Mirgating from Java
to Kotlin
I try to use static function with Data Binding
:
<data>
<import type="com.package.domain.tools.helper.StringValidator"/>
...
</data>
Then I call function hideNumber
:
<com.hastee.pay.ui.view.Text
...
android:text='@{StringValidator.hideNumber(account.number)}'
app:layout_constraintRight_toRightOf="@+id/number"
app:layout_constraintTop_toBottomOf="@+id/number" />
Using databinding here causes error:
[kapt] An exception occurred:
android.databinding.tool.util.LoggedErrorException: Found data binding
errors.
****/ data binding error ****msg:cannot find method
hideNumber(java.lang.String) in class
com.package.domain.tools.helper.StringValidator....
Here's this object:
object StringValidator {
...
fun hideNumber(number: String): String {
return "****" + number.substring(number.length - 4)
}
}
How can I reach this function using Kotlin
and Data Binding
?
The data binding compiler is looking for a static method.
Since a named object
alone is not enough to make all methods inside that object static
, you need an additional @JvmStatic
annotation on your hideNumber
-method:
@JvmStatic
fun hideNumber(number: String): String {
return "****" + number.substring(number.length - 4)
}
see also: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#static-methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With