3

I'm trying to use the SearchView Support v4 version with action bar sherlock.

So i have my search button in the action bar -> when i touch it the keyboard show up and the searchBar too.

My problem is that i need to use the listeners onQueryTextSubmit and onQueryTextChange but they are never fired. I need to use the searh query string and do custom stuff with it.

Here is the full activity.java

public class ActivityMain extends SherlockFragmentActivity implements OnQueryTextListener, DialogFragmentListener {
    /**
     * PRIVATE ATTRIBUTES
     */
    private static final String TAG                             = "ActivityMain";
    private ViewPager           _viewPager;
    private TabsAdapter         _tabsAdapter;
    private DialogFiltre        _dialogFiltre;
    private String              _searchCurrentQuery;
    // data
    private boolean             _doubleBackToExitPressedOnce    = false;


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        menu.clear();
        switch ((int) _viewPager.getCurrentItem()) {
            case 0:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
            case 1:
                getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
                break;
            case 2:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(queryTextListener);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {

        Log.i(TAG, "onQueryTextSubmit--");
        onSearchClicked(query);
        // hide keyboard
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        Log.d(TAG, "onQueryTextChange--");
        _searchCurrentQuery = newText.toString();
        EtablissementApplication._adapter.getFilter().filter(_searchCurrentQuery);
        return true;
    }

    private void onSearchClicked(String query) {

        Log.d(TAG, "onSearchClicked--");
        _searchCurrentQuery = query.toString();
        EtablissementApplication._adapter.getFilter().filter(_searchCurrentQuery);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                break;
            case R.id.search:
                break;
            case R.id.menu_filtre:
                _dialogFiltre = DialogFiltre.newInstance(R.string.menu_filtre, this);
                _dialogFiltre.setValidDialogListener(this);
                _dialogFiltre.show(getSupportFragmentManager(), null);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
4

3 回答 3

11

You are trying to use a new SearchView instead of using the one created by the SupportMenuInflater. You setting the listener to different SearchView that you see on the screen. Also, every time onPrepareOptionsMenu is called, new SearchView is created, and thus it has no listeners set.

Try to do onPrepareOptionsMenu like this:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    menu.clear();
    switch ((int) _viewPager.getCurrentItem()) {
        case 0:
            getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
            break;
        case 1:
            getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
            MenuItem searchItem = menu.findItem(R.id.search);
            SearchView searchView = (SearchView) searchItem.getActionView();
            searchView.setSubmitButtonEnabled(true);
            searchView.setOnQueryTextListener(queryTextListener);
            break;
        case 2:
            getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
            break;
    }
    return super.onPrepareOptionsMenu(menu);
}

And remove the overridden method

@Override
public boolean onCreateOptionsMenu(Menu menu) { }
于 2013-05-31T09:46:26.630 回答
1

Top result in Google so adding a new possible cause that just gave me a headache. Be mindful of which SearchView implementation you reference.

This did not work for me (compiles, does not crash, but listener does not work):

    <!--actionbar.xml-->
    ...
    <item android:id="@+id/search"
        android:title="@string/action_search"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.widget.SearchView"/>
    ...
    // Activity#onCreateOptionsMenu(android.view.Menu)
    val searchView: SearchView = menu.findItem(R.id.search).actionView as android.widget.SearchView
    searchView.setOnQueryTextListener(...)

This does (compiles, does not crash, and listener works):

    <!--actionbar.xml-->
    ...
    <item android:id="@+id/search"
        android:title="@string/action_search"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="androidx.appcompat.widget.SearchView"/>
    ...
    // Activity#onCreateOptionsMenu(android.view.Menu)
    val searchView: SearchView = menu.findItem(R.id.search).actionView as androidx.appcompat.widget.SearchView
    searchView.setOnQueryTextListener(...)
于 2021-06-25T15:13:56.233 回答
0

You may have to set searchView.setOnQueryTextListener(this); on your SearchView reference.

于 2019-03-30T17:27:27.503 回答