我想在我的 Android 应用程序中添加一个搜索 View ,但我无法理解文档。我已经添加了
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:includeInGlobalSearch="true"
android:searchSuggestAuthority="dictionary"
android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
到我的 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" />
到我的 list 。但是provider
应该放在哪里? -标记去?运行应用程序时出现类异常膨胀错误。
任何人都知道一个好的教程?谢谢!
最佳答案
这个答案很晚,但正如我所见,其他答案只是答案链接。然后,我将尝试用简短的示例代码提供一些解释。
添加 SearchView
在 Documentation 中描述,按照这些步骤操作非常容易。正如我们在 Create a Search Interface 上看到的那样主题:
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.
然后,您的 list 变为:
<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 中,因此您可能希望您的可搜索 Activity 扩展 ListActivity” - 仍然来自 Docs。所以你的 SearchActivity
可能是:
public class SearchActivity extends ListActivity { }
“当用户从搜索对话框或搜索小部件执行搜索时,系统会创建一个 Intent 并将用户查询存储在其中。然后系统会启动您声明的处理搜索的 Activity ( “可搜索的 Activity ”)并向其传递 Intent “。您需要使用 Intent
从搜索对话框或小部件中获取查询搜索在 onCreate
方法:
// 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()
将查询传递给 SearchActivity
的方法在 onOptionsItemSelected
方法。然后,您只需将此搜索项添加到您的菜单中,如下所示(不要忘记 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
的 Searchable Dictionary 演示并提供连接SQLite DataBase
的完整演示通过 Adapter
.
瞧!我希望您找到了解决方案,这篇文章将对有相同需求的人有所帮助。
关于android - 如何将搜索 View 添加到我的 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6213746/