Search code examples
androidandroid-contentprovidersearchviewsearch-suggestion

Custom ContentProvider suggestions not displaying


My goal: Implementing search within an application.

How it should work: I have multiple activities which have a SearchView within their Action Bar (or Toolbar as they now call it), and a dedicated Search activity. I want to be able to input text in the SearchView, receive suggestions while i am inputting text and when i send the search to launch the dedicated activity and do a proper listing of the results.

What i have done so far: I have gone with the SearchView with ContentAdapter method as suggested here. I have managed to have the SearchView in every activity, when i press go on the keyboard i am taken to the dedicated Search activity and the query is displayed (going to implement the effective search later). So far so good.

The problem: I have attempted to create a custom ContentProvider to provide some mock-up suggestions but i am unable to make it work. I define within it a custom String[], and then in the query method match my search with the elements in said string. The cursor is registered, and the query method fires when i am writing text but no results are displayed in the non-dedicated activities. In the dedicated Search activity i am shown an empty list-view and receive an "error changing cursor and caching columns" IllegalStateException

I am having trouble finding any information as everyone seems to use the ContentProvider with a local database. I however intend in the future to receive my information from a REST API in the query method and return the cursor using the received values (as in this example)

I would greatly appreciate any advice in this matter. As I am not sure what code to provide, I will provide it on request.


Solution

  • Fixed it...

    The problem was that I was creating my MatrixCursor in onCreate.

    The solution is to declare and instantiate the cursor in the query method.

    Side note: As it turns out, in the columns String[] that you pass to the MatrixCursor's constructor you need to specify the values using the SearchManager constants (ie SearchManager.SUGGEST_COLUMN_TEXT_1), otherwise the text will not be displayed within the suggestion view if you use the default view and adapter.

    I hope this helps someone.