gpt4 book ai didi

android - android Spinner 中的数据绑定(bind)错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:06 24 4
gpt4 key购买 nike

我正在尝试设置与 Android Spinner 的双向绑定(bind),但出现以下错误。

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

这是我的 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}"/>

最佳答案

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

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

对此:

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

反向绑定(bind)适配器来自:

    @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 - android Spinner 中的数据绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com