How to use DataBindingUtil with Android counter?

I am trying to use the new Android data binding library and get the following error while trying to populate the counter with the selected value.

Error message (during compilation in Android Studio):

Error: execution completed for task ': app: compileDebugJavaWithJavac'. java.lang.RuntimeException: Data binding errors detected. **** / data binding error **** msg: Unable to find the installer for the attribute "app: selection" with parameter type java.lang.String. file: /Users/ove/Code/AndroidStudio/Samples/Receipts/app/src/main/res/layout/dialogfragment_inputamount_db.xml loc: 40: 29 - 40:44 **** \ data binding error ****

My layout file looks like the following (not complete):

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="receipt" type="com.example.model.Receipt" /> </data> </LinearLayout> <Spinner android:layout_width="wrap_content" android:id="@+id/currency" android:layout_height="wrap_content" android:spinnerMode="dropdown" android:entries="@array/currency_array" app:selection="@{receipt.currency}" /> </LinearLayout> </layout> 

Anyone able to get data binding for working with spinners?

Ov

+6
source share
2 answers

create the BindingUtils class and insert the setSelection method

 public class BindingUtils { @BindingAdapter({"bind:selection"}) public static void setSelection(Spinner spinner, int position) { spinner.setSelection(position); } } 

inside spinner

 app:selection="@{receipt.currencyIdx}" 

That is all you have to do.

+2
source

The Spinner:setSelection inherited from AbsSpinner has an int parameter - not String :

public void setSelection(int position)

you must pass the position, not the value of the choice

 <Spinner android:layout_width="wrap_content" android:id="@+id/currency" android:layout_height="wrap_content" android:spinnerMode="dropdown" android:entries="@array/currency_array" app:selection="@{receipt.currencyIdx}" /> </LinearLayout> 
+1
source

All Articles