[英]Android Data-binding - Cannot find the getter for attribute
I used some spinner binding after seeing in this answer . 在看到此答案后,我使用了一些微调器绑定。 But i am not able to compile code after that. 但是之后我无法编译代码。
Error 错误
Found data binding errors. 发现数据绑定错误。 ****/ data binding error ****msg:Cannot find the getter for attribute 'selectedValue' with value type java.lang.String on android.widget.Spinner. **** /数据绑定错误**** msg:在android.widget.Spinner上找不到值为类型为java.lang.String的属性“ selectedValue”的获取器。 file:D:\\Khemraj_AndroidStudioWorkspace_\\amelioFinal\\app\\src\\main\\res\\layout\\activity_cart.xml loc:167:24 - 175:33 ****\\ data binding error **** 文件:D:\\ Khemraj_AndroidStudioWorkspace_ \\ amelioFinal \\ app \\ src \\ main \\ res \\ layout \\ activity_cart.xml loc:167:24-175:33 **** \\数据绑定错误****
Pojo Pojo
public class ViewModel {
private String text;
public String getText() {
return text;
}
public void setText(String text)
{
this.text = text;
}
}
in xml 在xml中
<variable
name="viewModel"
type="com.amelio.model.ViewModel"/>
and 和
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/days"
bind:selectedValue="@={viewModel.text}"/>
bindingUtil bindingUtil
public class SpinnerBindingUtil {
@BindingAdapter(value = {"selectedValue", "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 = "selectedValue", event = "selectedValueAttrChanged")
public static String captureSelectedValue(AppCompatSpinner pAppCompatSpinner) {
return (String) pAppCompatSpinner.getSelectedItem();
}
}
There is an android:selectedItemPosition
attribute that supports two-way data binding out of the box. 有一个android:selectedItemPosition
属性,支持开箱即用的双向数据绑定。 You can use that or use its BindingAdapter
/ InverseBindingAdapter
pair as a template: 您可以使用它,也可以将其BindingAdapter
/ InverseBindingAdapter
对用作模板:
@InverseBindingMethods({
@InverseBindingMethod(type = AdapterView.class, attribute = "android:selectedItemPosition"),
@InverseBindingMethod(type = AdapterView.class, attribute = "android:selection",
method = "getSelectedItemPosition",
event = "android:selectedItemPositionAttrChanged"),
})
... ...
@BindingAdapter("android:selectedItemPosition")
public static void setSelectedItemPosition(AdapterView view, int position) {
if (view.getSelectedItemPosition() != position) {
view.setSelection(position);
}
}
@BindingAdapter(value = {"android:onItemSelected", "android:onNothingSelected",
"android:selectedItemPositionAttrChanged" }, requireAll = false)
public static void setOnItemSelectedListener(AdapterView view, final OnItemSelected selected,
final OnNothingSelected nothingSelected, final InverseBindingListener attrChanged) {
if (selected == null && nothingSelected == null && attrChanged == null) {
view.setOnItemSelectedListener(null);
} else {
view.setOnItemSelectedListener(
new OnItemSelectedComponentListener(selected, nothingSelected, attrChanged));
}
}
That would lead me to do something like this: 那会导致我做这样的事情:
@InverseBindingMethods({
@InverseBindingMethod(type = AdapterView.class,
attribute = "android:selectedValue",
method = "getSelectedItem"
})
public class SelectedValueBindingAdapter {
@BindingAdapter("android:selectedValueAttrChanged")
public static void setSelectedValueListener(AdapterView view,
final InverseBindingListener attrChanged) {
if (attrChanged == null) {
view.setOnItemSelectedListener(null);
} else {
view.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
attrChanged.onChange();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
attrChanged.onChange();
}
});
}
}
@BindingAdapter("android:selectedValue")
public static void setSelectedValue(AdapterView<?> view, Object selectedValue) {
Adapter adapter = view.getAdapter();
if (adapter == null) {
return;
}
// I haven't tried this, but maybe setting invalid position will
// clear the selection?
int position = AdapterView.INVALID_POSITION;
for (int i = 0; i < adapter.getCount(); i++) {
if (adapter.getItem(i) == selectedValue) {
position = i;
break;
}
}
view.setSelection(position);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.