Home » Android » android – Databinding: conditional onclick call-Exceptionshub

android – Databinding: conditional onclick call-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

From my layout, when a user tap a view i need to call different method based on a boolean value passed to layout.

i’ve something like:

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="item"
            type="com.example.MyObject" />

        <variable
            name="viewmodel"
            type="com.example.MyViewModel" />

        <variable
            name="aBolean"
            type="Boolean" />
    </data>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:onClick="@{()-> aBoolean ? viewmodel.methodOne() : viewmodel.methodTwo()}"
      />

</layout>

but when i build my code, i obtain an error:

error: variable aBolean is already defined in method _internalCallbackOnClick(int,View)

how can i do?

How to&Answers: