質問

I have an database with my Values that should be a possible search option. And I have my UI with an Search box in Action bar. Now I like to show a List that contains all possible Values that begin with the written letters in the search box.

Whenever I try to find a tutorial I only get RecentSearchSuggestions but I like to use something like a CustomCursor directly in my main Activity without having to go to a new one.

Is there a possibility to achive that? A link to a tutorial or some hints are totally enough.

役に立ちましたか?

解決

It`s easy, do something like this: (it's pseudocode)

 searchView.setOnQueryChangeListener(new OnQueryChangeListener() {

      public void onQueryChanged(String query) {
          Cursor suggestions = dbHelper.getSuggestionsByQuery(query); 
          //dbHelper is object that handles interactions with your DB
          SimpleCursorAdapter suggestionsAdapter = new SimpleCursorAdapter ... ;
          searchView.setSuggestionsAdapter(suggestionsAdapter);
      }
 });

他のヒント

Why not just issue a query to your DB when onTextChanged is called? You could do something like:

 SELECT values_table.values
 FROM values_table
 WHERE values LIKE 'parameter%'

You can then dump those results into the search suggestions box however you see fit. Also, you could try reading this article which seems to give a whole bunch of detail about how to implement custom search suggestions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top