I am trying to use the new Android data binding library and get the following error trying to populate a spinner with the selected value.
Error Message (during compilation in Android Studio):
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for 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 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 out there that has managed to get data binding to work with spinners?
Ove
create a class BindingUtils and paste 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}"
Thats all you have to do.
The setter Spinner:setSelection
inherited from AbsSpinner
has int
parameter - not String
:
public void setSelection(int position)
you should pass a position, not the value of the selection
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With