Goal
Enter keywords and display search results in real time.
Author: Muniao Notes https://www.qtmuniao.com. Please indicate the source when reposting.
Process
Starting from the official documentation, since I was a beginner and knew few related terms, and the document was not a code-level implementation, I failed to build a complete search framework. The document mainly covers the following points:
- Two implementation methods: search dialog and search widget; for devices running Android 3.0 and later, the latter is recommended as it is more flexible.
- Three main components:
- Searchable configuration (a searchable configuration)
- Searchable container (a searchable Activity) (Confusion point 1)
- Search interface (a search interface)
- Search process:
- Accept the query (Receive the query)
Using Intent (Confusion point 2) - Search data (Search your data)
- Present results (Present the results) (Confusion point 3)
- Accept the query (Receive the query)
I wanted to use the Search Widget approach and encountered the following confusion points:
- How to trigger a search in
Activity—that is, how to make the search icon appear in the upper-right corner of the AppBar. - If using
Intentto query data, as in the examples, it should not be possible to match input characters in real time. I guessed there should be something like a listener, but the example didn’t provide one. - How to present results: the documentation suggests having
SearchableActivityinherit fromListView, but specific details, such as how to receive results and pass them toListView, were not mentioned.
In short, none of the three steps in the search process was clear to me; I was completely baffled.
So I searched for the keywords “SearchView action bar” and found a post: Implementing SearchView in action bar. After studying it carefully, I figured out the above issues.
First, regarding triggering the search, the answer uses an Activity with an App Bar as the SearchableActivity, overrides the onCreateOptionsMenu function, instantiates its menu parameter, and adds the SearchView as one of its items. This way, a search button appears in the upper-right corner of the SearchableActivity. The relevant code is as follows:
res\menu\search.xml:
1 |
|
SearchableActivity.java
1 |
|
AndroidManifest.xml
1 | <activity android:name=".SearchableActivity"> |
res/xml/searchable.xml
1 | <?xml version="1.0" encoding="utf-8"?> |
Second, for real-time matching of query results: this is also done in the onCreateOptionsMenu function by setting listeners for the SearchView. See the code above for details, but I haven’t yet figured out the difference between returning true and false.
Finally, displaying data: as the official documentation says, use ListView. The specific approach is to pass data (e.g., List<String>) to a ListView inflated from XML via an Adapter. The answer uses CursorAdapter (more suitable for database data). The specific code is as follows:
res/layout/item.xml
1 | <?xml version="1.0" encoding="utf-8"?> |
ResultAdapter.java
1 | public class ResultAdapter extends CursorAdapter { |
SearchableActivity.java
1 | private void doMySearch(String query){ |
References:
- Android Official Documentation, https://developer.android.com/guide/topics/search/search-dialog.html
- Stack Overflow Answer, https://stackoverflow.com/questions/21585326/implementing-searchview-in-action-bar
- Official Video, https://www.youtube.com/watch?v=9OWmnYPX1uc
