12

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 !

Community
  • 1
  • 1
MHogge
  • 5,408
  • 15
  • 61
  • 104
  • 2
    The error suggest that you are missing the setter for `OnQueryTextListener` - do you have one? You need both setter & getter for databinding. – yennsarah Dec 01 '15 at 09:20
  • The SearchView class has no getter for the QueryTextListener but as it is described in the related post, it seems possible to do the same thing but with an EditText and the EditText class does not have any getter for the TextWatcher neither. – MHogge Dec 01 '15 at 09:29
  • I''m not talking about a getter, but about a setter. So I think you are missing a method `setQueryTextListener(QueryTextListener mQueryTextListener){...}` – yennsarah Dec 01 '15 at 09:32
  • Sorry, misunderstanding. The setter can be found in the doc of the SearchView class : [here](http://developer.android.com/reference/android/widget/SearchView.html#setOnQueryTextListener(android.widget.SearchView.OnQueryTextListener)) – MHogge Dec 01 '15 at 09:35
  • Sure it can be found there, but for databinding, you'll need a setter in your `MembersFragmentViewModel` class, because you are calling it from your xml. – yennsarah Dec 01 '15 at 09:42
  • I don't think this is the problem but to be sure I added the setter in `MembersFragmentViewModel` and the same error occurs. – MHogge Dec 01 '15 at 09:52
  • Can you add the code? And maybe try add `@Bindable` to your `getQueryTextListener(){...}` method. – yennsarah Dec 01 '15 at 09:58
  • 1
    Another try would be to add `@BindingAdapter(bind:setOnQueryTextListener)` above your method, since this is a custom method, [documentation](http://developer.android.com/tools/data-binding/guide.html#custom_setters) I'm sorry I couldn't help you any further. – yennsarah Dec 01 '15 at 10:13
  • fyi for binding problems just do CMD + SHFT + O . then type the class binding like SearchViewBindingAdapter and you will find everything they expose that you can use. – Sam Oct 20 '17 at 15:01

1 Answers1

5

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
MHogge
  • 5,408
  • 15
  • 61
  • 104