【问题标题】:DataBinding error in android Spinnerandroid Spinner 中的数据绑定错误
【发布时间】:2017-11-09 22:56:43
【问题描述】:

我正在尝试设置与 Android Spinner 的两种方式绑定,但出现以下错误。

原因:android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。 ****/ 数据绑定错误 ****msg:无法在 android.widget.Spinner****\ 数据绑定错误上找到属性 'bind:selectedValue' 的 getter,其值类型为 java.lang.String *

这是我的 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();
    }
}

这是我的 ViewModel(这是在 Kotlin 中)

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

布局 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}"/>

【问题讨论】:

    标签: android kotlin android-spinner android-databinding


    【解决方案1】:

    从以下位置转换您的绑定适配器:

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

    到这里:

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

    和反向绑定适配器来自:

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

    到这里:

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

    在你的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}"/>
    

    记住将你的根布局标签更新为:

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    

    【讨论】:

    • 还是同样的错误。它现在说的是应用程序而不是绑定。我认为问题不在于绑定。
    • 该死的错误很简单。它在 android.widget.Spinner**** 上显示值类型为 java.lang.String 的“bind:selectedValue”。您期待字符串,但您正在发送 ObservableField。只需发送 bind:selectedValue="@={viewModel.selectedState.get()}"
    猜你喜欢
    • 2011-10-17
    • 2011-09-27
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多