Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 509
MHogge Avatar asked Dec 01 '15 09:12

MHogge


1 Answers

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
like image 51
MHogge Avatar answered Nov 17 '22 18:11

MHogge