Je suis en train d'essayer de configurer une liaison bidirectionnelle avec Android Spinner mais je reçois l'erreur suivante.
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 ****
Voici mon 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) 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();
}
}
et c'est mon ViewModel (c'est en Kotlin)
class AddressDetailsViewModel : ViewModel() {
val states: ObservableArrayList = ObservableArrayList()
val selectedState: ObservableField = ObservableField("State")
}
layout XML: