context: data binding with a ViewModel, which gets data from a remote source in the form of JSON. I want to display a textual value from that JSON in a TextView, but if the data is absent in the JSON, I want to fall back to a string defined in strings.xml.
android:text="@{viewModel.theText}"
How I currently solved it is with a custom binding adapter that accepts an Any
, and checks if the value is an Int or String:
app:anyText="@{viewModel.theText}"
. The viewModel has something like val theText = json.nullableString ?: R.string.placeholder
.
I'm guessing that this is a problem more people deal with, and I was hoping if someone knows a more elegant solution.