简体   繁体   English

Android数据绑定:无法找到属性的setter

[英]Android Data Binding : can't find the setter for attribute

Not sure how it works but according to this post it's possible to bind specific listener by using different namespaces. 不确定它是如何工作的但是根据这篇文章 ,可以通过使用不同的命名空间来绑定特定的监听器。

I wanted to do the same thing with a searchview and bind a QueryTextListener to it but I get the following error : 我想用searchview做同样的事情并将QueryTextListener绑定到它但我收到以下错误:

Cannot find the setter for attribute 'bind:setOnQueryTextListener' 
with parameter type android.widget.SearchView.OnQueryTextListener. 

What I did in my ViewModel : 我在ViewModel中做了什么:

public class MembersFragmentViewModel extends BaseObservable {

    private Context context;
    private MembersAdapter adapter;
    private RecyclerView recyclerView;

    public MembersFragmentViewModel(Context context, MembersAdapter adapter, RecyclerView recyclerView) {
        this.context = context;
        this.adapter = adapter;
        this.recyclerView = recyclerView;
    }

    public SearchView.OnQueryTextListener getQueryTextListener(){
        return new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                List<Contact> filteredModelList = filter(adapter.getContacts(), query);
                adapter.animateTo(filteredModelList);
                if(recyclerView != null)
                    recyclerView.scrollToPosition(0);
                return true;
            }
        };
    } 
    //Code ...

And the xml : 和xml:

My namespace is declared in the layout tag like this : 我的命名空间在布局标记中声明如下:

xmlns:bind="http://schemas.android.com/apk/res-auto"

And my SearchView : 而我的SearchView:

<android.support.v7.widget.SearchView android:id="@+id/searchview"
        android:layout_width="match_parent"
        android:layout_height="40dp"

        android:background="@drawable/rounded_search_view_background"
        bind:setOnQueryTextListener="@{viewModel.QueryTextListener}">

</android.support.v7.widget.SearchView>

This is my data tag : 这是我的数据标签:

<data>
   <variable
       name="viewModel"
       type="mypackagename.viewmodel.members.MembersFragmentViewModel"/>
</data>

Many thanks for any clue ! 非常感谢任何线索!

In my case the answer was just a wrong package name. 在我的情况下,答案只是一个错误的包名。

My SearchView was declared in xml with the following package name : 我的SearchView在xml中使用以下包名称声明:

android.support.v7.widget.SearchView

And the package I used in the ViewModel was 我在ViewModel中使用的包是

android.widget.SearchView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android数据绑定:找不到属性android:onClick的setter - Android data binding: can't find setter for attribute android:onClick Android数据绑定:找不到属性app:list的setter - Android data binding : Cannot find setter for attribute app:list 在数据绑定中找不到属性的设置器 - Cannot find the setter for attribute in Data binding Android数据绑定BindingAdapter:“找不到设置器” - Android Data Binding BindingAdapter: “Cannot find the setter” AndroidX迁移-数据绑定错误msg:找不到参数类型为int的属性“ android:visibility”的设置器 - AndroidX migration - data binding error msg:Cannot find the setter for attribute 'android:visibility' with parameter type int 数据绑定时的自定义属性设置器错误 - Android - custom attribute setter error when data binding - Android ****/ 数据绑定错误 ****msg:在 android.widget.ImageView 上找不到参数类型为 java.lang.String 的属性“app:image_url”的设置器 - ****/ data binding error ****msg:Cannot find the setter for attribute 'app:image_url' with parameter type java.lang.String on android.widget.ImageView 找不到属性“ android:text”的设置器 - Cannot find the setter for attribute 'android:text' Android找不到属性错误的设置器 - Android Cannot find the setter for attribute Error Android数据绑定-找不到属性的获取器 - Android Data-binding - Cannot find the getter for attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM