这个答案很晚,但正如我所见,其他答案只是 answer-links。然后,我将尝试用简短的示例代码提供一些解释。
在Documentation 中描述了添加SearchView,并且很容易按照这些步骤进行操作。正如我们在Create a Search Interface 主题上看到的那样:
<intent-filter>不需要带有 DEFAULT 值的<category>(您通常在<activity>elements 中看到),因为系统提供了ACTION_SEARCH Intent 明确地指向您的可搜索活动,使用其组件名称。
然后,你的清单变成:
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/my_searchable_layout"/>
“传统上,您的搜索结果应该显示在 ListView 中,因此您可能希望您的可搜索活动扩展 ListActivity” - 仍然来自 Docs。所以你的SearchActivity 可能是:
public class SearchActivity extends ListActivity { }
“当用户从搜索对话框或搜索小部件执行搜索时,系统会创建一个 Intent 并将用户查询存储在其中。然后系统会启动您声明的用于处理搜索的活动 ( “可搜索的活动”)并将其传递给意图”。您需要通过在onCreate 方法中使用Intent 从搜索对话框或小部件中获取查询搜索:
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// Receive the query
String query = intent.getStringExtra(SearchManager.QUERY);
// Search method..
doMySearch(query);
}
doMySearch() 可以是一个AsyncTask,一个新的Thread.. 连接到一个SQLite DataBase,一个SharedPreference,等等...... “存储和搜索数据的过程是独一无二的您的应用程序”。话虽如此,您应该创建一个Adapter 以在列表中提供您的结果。
这是一个简短的ListActivity 示例,用于SearchActivity 使用异步任务填充列表:
public class SearchActivity extends ListActivity {
// Create an array
String[] values;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_layout);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// At the end of doMySearch(), you can populate
// a String Array as resultStringArray[] and set the Adapter
doMySearch(query);
}
}
class doMySearch extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
// Connect to a SQLite DataBase, do some stuff..
// Populate the Array, and return a succeed message
// As String succeed = "Loaded";
return succeed;
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Loaded") {
// You can create and populate an Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SearchActivity.this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
}
}
最后,我更喜欢将SearchView 小部件与AppCompat 或ActionBarSherlock 一起使用,它描述了主题的at the end。自从你问了你的问题(3 年前^^)之后,我认为它更适应了。所以,要做:
// Example with AppCompat
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu
getMenuInflater().inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Iconify the widget
return true;
}
并执行startActivity() 方法将查询传递给onOptionsItemSelected 方法中的SearchActivity。然后,您只需将此搜索项添加到您的菜单中,如下所示(不要忘记custom prefix),就像您在Adding an Action View 上看到的那样:
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
在sample Docs 中,您有可搜索字典 演示,其中包含ListView,并提供通过Adapter 连接SQLite DataBase 的完整演示。
瞧!我希望你找到了解决方案,这篇文章将帮助那些想要同样的人。