Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding - How to write multi-line declarations?

Tags:

Is there any way to write multi-line code with DataBinding library?

I tried following, but it complains about XML format

android:text="@{             viewModel.subscriptionExpiration == null                 ? viewModel.field1                 : viewModel.field2         }" 

With following error: Error:(80) Error parsing XML: not well-formed (invalid token)

Single-line alternative works, but I find it less readable:

android:text="@{viewModel.subscriptionExpiration == null ? viewModel.field1 : viewModel.field2}" 
like image 438
Dmitry Zaytsev Avatar asked Nov 04 '15 09:11

Dmitry Zaytsev


People also ask

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.

How do you use data binding with tag?

Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.

What is data binding with example?

Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.


1 Answers

I tried doing some digging and couldn't find an answer. I don't know that you can break up string literals that way in xml. As an alternative I would recommend offloading the evaluation to the viewModel instead, in the form of:

android:text="@{viewModel.subscriptionStatus}" 

then in your viewModel class

@Bindable public String getSubscriptionStatus(){     return getSubscriptionExpiration() == null             ? getField1()             : getField2(); } 
like image 174
SeptimusX75 Avatar answered Sep 18 '22 15:09

SeptimusX75