does anyone know if it's possible to pass a functi...
# android
j
does anyone know if it's possible to pass a function reference using databinding? specifically, I wanted to do something like this:
android:text="@{viewModel.message(context::getString)}"
where my
viewModel
class has a function like:
fun message(getStringFunc: (Int) -> String)
g
have you tried? it’s more about method reference implementation of bindings
j
yeah I tried, the data binding code gen fails
g
and what is error message?
j
when it's trying to generate, it's looking for a
message
function in the viewModel whose parameter is the wrong type (the type is as above, and it's looking for
fun message(object: Object)
)
g
looks as some problem of databindings, I would report to Android issue tracker
j
you can, but the "context::getString" part won't work. Context shouldn't be pass. replace it with something like viewModel.message(messageRes) where messeRes is a variable bind in your xml
g
you can, but the “context::getString” part won’t work
Isn’t this the whole point of this question. To pass some abstract function instead of exposing context. I’m not sure that it’s good enough solution, usually we provide own abstraction for resources to ViewModel, but imo using function instead of exposing context is better
But yeah, use resource is also good, but not always possible