Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding error in android Spinner

I am trying to setup two way binding with Android Spinner but I am getting following error.

Caused by: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Cannot find the getter for attribute 'bind:selectedValue' with value type java.lang.String on android.widget.Spinner****\ data binding error ****

This is my SpinnerBindingUtils

public class SpinnerBindingUtils {

    @BindingAdapter(value = {"bind:selectedValue", "bind:selectedValueAttrChanged"},
        requireAll = false)
    public static void bindSpinnerData(AppCompatSpinner pAppCompatSpinner, String newSelectedValue,
        final InverseBindingListener newTextAttrChanged) {
        pAppCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                newTextAttrChanged.onChange();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        if (newSelectedValue != null) {
            int pos = ((ArrayAdapter<String>) pAppCompatSpinner.getAdapter()).getPosition(newSelectedValue);
            pAppCompatSpinner.setSelection(pos, true);
        }
    }

    @InverseBindingAdapter(attribute = "bind:selectedValue",
        event = "bind:selectedValueAttrChanged")
    public static String captureSelectedValue(AppCompatSpinner pAppCompatSpinner) {
        return (String) pAppCompatSpinner.getSelectedItem();
    }
}

and this is My ViewModel(This is in Kotlin)

class AddressDetailsViewModel : ViewModel() {
   val states: ObservableArrayList<String> = ObservableArrayList()
   val selectedState: ObservableField<String> = ObservableField("State")
}

layout XML:

<Spinner
     android:id="@+id/state_address_details"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="8dp"
     android:layout_weight="1"
     android:entries="@{viewModel.states}"
     bind:selectedValue="@={viewModel.selectedState}"/>
like image 561
OkDroid Avatar asked Nov 07 '22 14:11

OkDroid


1 Answers

Convert your binding adapter from:

@BindingAdapter(value = {"bind:selectedValue", "bind:selectedValueAttrChanged"},
    requireAll = false)

To this:

@BindingAdapter(value = {"selectedValue", "selectedValueAttrChanged"},
    requireAll = false)

And inverse binding adapter from:

    @InverseBindingAdapter(attribute = "bind:selectedValue",
    event = "bind:selectedValueAttrChanged")

To this:

@InverseBindingAdapter(attribute = "selectedValue",
    event = "selectedValueAttrChanged")

And in your xml you can do this:

<Spinner
 android:id="@+id/state_address_details"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="8dp"
 android:layout_weight="1"
 android:entries="@{viewModel.states}"
 app:selectedValue="@={viewModel.selectedState}"/>

Remeber to update your root layout tag to this:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
like image 76
sanoop sandy Avatar answered Nov 15 '22 08:11

sanoop sandy