在this answer中看到后,我使用了一些微调器绑定。但是之后我无法编译代码。
错误
发现数据绑定错误。 **** /数据绑定错误****消息:无法找到属性" selectedValue'的getter在android.widget.Spinner上使用值类型java.lang.String。 文件:d:\ Khemraj_AndroidStudioWorkspace_ \ amelioFinal \程序\ SRC \主\水库\布局\ activity_cart.xml 当前:167:24 - 175:33 **** \数据绑定错误****
POJO的
public class ViewModel {
private String text;
public String getText() {
return text;
}
public void setText(String text)
{
this.text = text;
}
}
in xml
<variable
name="viewModel"
type="com.amelio.model.ViewModel"/>
和
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/days"
bind:selectedValue="@={viewModel.text}"/>
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();
}
}
答案 0 :(得分:1)
有一个android:selectedItemPosition
属性支持开箱即用的双向数据绑定。您可以使用它或使用其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));
}
}
这会让我做这样的事情:
@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);
}
}