简体   繁体   中英

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 :

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

What I did in my 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 :

My namespace is declared in the layout tag like this :

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

And my 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 :

android.support.v7.widget.SearchView

And the package I used in the ViewModel was

android.widget.SearchView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM