0

I have a custom search implemented using SearchView (ActionBar) with a custom adapter and provider. The problem I have is that the query() method of the ContentProvider is called only once, first time I input something in the SearchView! If I continue typing in the view the query is not called so that the list of suggestions does not get updated.

In a fragment I have where SearchView init:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.planning, menu);
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.searchAirportItem).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setOnQueryTextListener(this);
        searchView.setOnSuggestionListener(this);
        airportSearchAdapter = new AirportSearchAdapter(getActivity());
        searchView.setSuggestionsAdapter(airportSearchAdapter);
    }

The suggestion adapter AirportSearchAdapter only redefine the layout of suggested items.

The Fragment implements the QueryTextListener and the method onQueryTextChange looks like

@Override
public boolean onQueryTextChange(String newText) {
    Bundle data = new Bundle();
    data.putString("query", newText);
    getActivity().getLoaderManager().initLoader(1, data, this);
    return true;
}

The Fragment also implements LoaderCallbacks:

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri uri = AirportsContentProvider.CONTENT_URI;
        return new CursorLoader(getActivity(), uri, null, null, new String[] { args.getString("query") }, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        airportSearchAdapter.changeCursor(data);
    }`

Any idea why the ContentProvider query method is called only once? Not each time I type?

cmg_george
  • 309
  • 1
  • 2
  • 9
  • it works as intended ... initLoader <= is the key ... read the documentation – Selvin Oct 06 '15 at 14:10
  • Sorry it was to so clear from documentation that this should be the behavior. I end up using getActivity().getLoaderManager().restartLoader(1, data, this); – cmg_george Oct 06 '15 at 15:00
  • *I end up using getActivity().getLoaderManager().restartLoader(1, data, this);* :) ... initLoader is good when you fx: rotate the screen and you don't wana requery(look out: support:23.0.1 f* it up) ... if you wana requery then restartLoader is the way ... **so, yes, "ending with restartLoader" is a good solution** – Selvin Oct 06 '15 at 15:39

1 Answers1

0

As Selvin suggested the problem was the initLoader() method.

Correct line is:

getActivity().getLoaderManager().restartLoader(1, data, this);
cmg_george
  • 309
  • 1
  • 2
  • 9