[英]How can I add a searchview to my Android app?
I want to add a searchview to my Android app but I can't understand the documentation.我想在我的 Android 应用程序中添加一个搜索视图,但我无法理解文档。 I have added
我已经添加了
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:includeInGlobalSearch="true"
android:searchSuggestAuthority="dictionary"
android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
to my xml and <intent-filter>
到我的 xml 和
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" /
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
to my manifest.我的清单。 But where should the
provider
-tag go?但是
provider
应该在哪里标记 go? i get a error inflating class exception when running the app.运行应用程序时,我收到错误膨胀 class 异常。 Anyone know of a good tutorial?
有谁知道好的教程吗? THanks!
谢谢!
This answer is quite later, but as I can see, other answers are only answer-links .这个答案很晚,但正如我所见,其他答案只是answer-links 。 Then, I will try to provide some explainations with short example code.
然后,我将尝试用简短的示例代码提供一些解释。
Adding a SearchView
is described in the Documentation , and it's quite easy to follow the steps. 文档中描述了添加
SearchView
,按照这些步骤操作非常容易。 As we can read on Create a Search Interface topic:正如我们在创建搜索界面主题中所读到的:
The
<intent-filter>
does not need a<category>
with the DEFAULT value (which you usually see in<activity>
elements), because the system delivers the ACTION_SEARCH intent explicitly to your searchable activity, using its component name.<intent-filter>
不需要具有DEFAULT值的<category>
(您通常在<activity>
元素中看到),因为系统使用其组件名称将ACTION_SEARCH意图明确地传递给您的可搜索活动。
Then, your manifest become:然后,您的清单变为:
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/my_searchable_layout"/>
"Traditionally, your search results should be presented in a ListView, so you might want your searchable activity to extend ListActivity" - still from Docs. “传统上,您的搜索结果应该显示在 ListView 中,因此您可能希望您的可搜索活动扩展 ListActivity” - 仍然来自 Docs。 So your
SearchActivity
might be:所以你的
SearchActivity
可能是:
public class SearchActivity extends ListActivity { }
"When the user executes a search from the search dialog or a search widget, the system creates an Intent and stores the user query in it. The system then starts the activity that you've declared to handle searches (the "searchable activity") and delivers it the intent" . “当用户从搜索对话框或搜索小部件执行搜索时,系统会创建一个 Intent 并将用户查询存储在其中。然后系统会启动您声明的用于处理搜索的活动(“可搜索活动”)并传达它的意图” 。 You need to get the query search from the search dialog or widget by using an
Intent
in onCreate
method:您需要使用
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()
can be an AsyncTask
, a new Thread
.. connected to a SQLite DataBase
, a SharedPreference
, whatever.. "The process of storing and searching your data is unique to your application" . doMySearch()
可以是AsyncTask
、新Thread
.. 连接到SQLite DataBase
、 SharedPreference
等等。 “存储和搜索数据的过程对于您的应用程序来说是独一无二的” 。 This being said, you should create an Adapter
to provide your results in the list.话虽如此,您应该创建一个
Adapter
以在列表中提供您的结果。
Here is a short ListActivity
example for SearchActivity
using an asynctask to populate a list:下面是一个简短的
ListActivity
示例,用于SearchActivity
使用 asynctask 填充列表:
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);
}
}
}
}
Finally, I prefer to use a SearchView
widget with the AppCompat or ActionBarSherlock and it describes at the end of the topic.最后,我更喜欢将
SearchView
小部件与AppCompat或ActionBarSherlock一起使用,它在主题末尾进行了描述。 It's I think more adapted since you asked your question (3 years ago ^^) .自从你问了你的问题(3年前^^)以来,我认为它更适应了。 So, to do:
所以,要做:
// 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;
}
And perform a startActivity()
method to pass the query to SearchActivity
in onOptionsItemSelected
method.并执行
startActivity()
方法将查询传递给onOptionsItemSelected
方法中的SearchActivity
。 Then, you will just to add this search item into your menu as follows (don't forget the custom prefix ) like you can see on 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" />
In sample Docs , you have the Searchable Dictionary demo which contains a ListView
and provide a full demo of connecting SQLite DataBase
via an Adapter
.在示例文档中,您有包含
ListView
的Searchable Dictionary演示,并提供了通过Adapter
连接SQLite DataBase
的完整演示。
Voila.瞧。 I hope you found the solution yet and this post will help someone which want the same.
我希望你找到了解决方案,这篇文章将帮助那些想要同样的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.