安卓Spinner中的数据绑定错误(DataBinding error)

5
我正在尝试使用Android Spinner进行双向绑定,但是我遇到了以下错误。
原因:android.databinding.tool.util.LoggedErrorException: 发现数据绑定错误。 ****/ 数据绑定错误 **** 消息:无法找到属性“bind:selectedValue”的getter方法,其值类型为java.lang.String,位于android.widget.Spinner上。 ****\ 数据绑定错误 ****
这是我的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}"/>
1个回答

1

将您的绑定适配器从以下形式转换:

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

To this:

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

将双向数据绑定反转的适配器:

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

To this:

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

在你的xml中,你可以这样做:

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

请记得将您的根布局标签更新为以下内容:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

1
仍然是同样的错误。现在它说的是“app”而不是“bind”。我认为问题不在于“bind”。 - OkDroid
该错误非常简单。它说在android.widget.Spinner****上有一个值类型为java.lang.String的'bind:selectedValue'。您期望的是字符串,但您正在发送ObservableField<String>。只需发送bind:selectedValue="@={viewModel.selectedState.get()}"即可。 - sanoop sandy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接