简体   繁体   English

Android数据绑定-找不到属性的获取器

[英]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.

相关问题 msg:在数据绑定中找不到值为java.lang.String的属性&#39;android:text&#39;的getter? - msg:Cannot find the getter for attribute 'android:text' with value type java.lang.String in data binding? Android 数据绑定 - 找不到接受参数类型“long”的 &lt;&gt; 的 getter - Android data binding - cannot find getter for <> that accepts parameter type 'long' 如何解决:“在使用自定义视图实现双向数据绑定时,找不到属性&#39;android:text&#39;”的getter? - How to solve: “cannot find getter for attribute 'android:text'” when implementing two-way data binding with custom view? 空 Android 数据绑定 - Null Android data-binding 找不到属性“ android:tag”的获取器-Android - Cannot find the getter for attribute 'android:tag' - Android 找不到属性“ android:checked”的吸气剂 - Cannot find the getter for attribute 'android:checked' Android kotlin 数据绑定与片段错误:找不到符号导入 com.example.***.databinding.FragmentUserBindingImpl; - Android kotlin data-binding with fragments error:cannot find symbol import com.example.***.databinding.FragmentUserBindingImpl; Android数据绑定:找不到属性app:list的setter - Android data binding : Cannot find setter for attribute app:list 使用数据绑定时在 xml 中找不到属性“android:onTextChanged” - Cannot find attribute "android:onTextChanged" in xml when using data binding 双向数据绑定 Android。 找不到接受参数类型“”的 &lt;&gt; 的 getter - Two-way data binding Android. Cannot find a getter for <> that accepts parameter type ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM