android Spinner中的DataBinding错误

OkD*_*oid 5 android android-spinner kotlin android-databinding

我试图设置与Android Spinner的双向绑定,但我收到以下错误.

引起:android.databinding.tool.util.LoggedErrorException:发现数据绑定错误.****/数据绑定错误****msg:找不到属性'bind:selectedValue'的getter,android.widget.Spinner上的值类型为java.lang.String****\data binding error****

这是我的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的ViewModel(这是在Kotlin)

class AddressDetailsViewModel : ViewModel() {
   val states: ObservableArrayList<String> = ObservableArrayList()
   val selectedState: ObservableField<String> = ObservableField("State")
}
Run Code Online (Sandbox Code Playgroud)

布局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}"/>
Run Code Online (Sandbox Code Playgroud)

小智 1

将您的绑定适配器转换为:

@BindingAdapter(value = {"bind:selectedValue", "bind:selectedValueAttrChanged"},
    requireAll = false)
Run Code Online (Sandbox Code Playgroud)

对此:

@BindingAdapter(value = {"selectedValue", "selectedValueAttrChanged"},
    requireAll = false)
Run Code Online (Sandbox Code Playgroud)

以及反向绑定适配器来自:

    @InverseBindingAdapter(attribute = "bind:selectedValue",
    event = "bind:selectedValueAttrChanged")
Run Code Online (Sandbox Code Playgroud)

对此:

@InverseBindingAdapter(attribute = "selectedValue",
    event = "selectedValueAttrChanged")
Run Code Online (Sandbox Code Playgroud)

在你的 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}"
 app:selectedValue="@={viewModel.selectedState}"/>
Run Code Online (Sandbox Code Playgroud)

请记住将根布局标记更新为:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
Run Code Online (Sandbox Code Playgroud)