Issue
I am trying to set text in TextView using data binding. I am using if else statement and if the value is true I want to set String to that TextView and in the other case I want to assign id of the String resource. My Code:
android:text="@{object.isTrue ? object.getString : object.getStringId}"
But when I try to do it I get error that Integer cannot be converted to String.
Everything is alright when I try to assign this String resource it directly like this:
android:text="@{object.getStringId}"
Is it somehow possible to use in that if else statement two different return types?
Solution
Yes, you can use Context.getString() on the second one, so that both are a String. I think this should work:
android:text="@{object.isTrue ? object.getString : context.getString(object.getStringId)}"
You don't have to import context, it's auto imported.
Answered By - Frank
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.