如何在我的Android应用中添加搜索视图?

kak*_*a47 4 search android

我想在我的Android应用中添加一个searchview,但我无法理解文档.我已经添加了

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
       android:includeInGlobalSearch="true"
       android:searchSuggestAuthority="dictionary"
       android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
Run Code Online (Sandbox Code Playgroud)

到我的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" />
Run Code Online (Sandbox Code Playgroud)

我的清单.但是provider-tag 应该去哪里?运行应用程序时,我收到一个错误膨胀类异常.有人知道一个很好的教程吗?谢谢!

fll*_*llo 8

这个答案要晚得多,但正如我所看到的,其他答案只是答案链接.然后,我将尝试用简短的示例代码提供一些解释.文档中描述了
添加a ,并且按照步骤操作非常简单.我们可以阅读创建搜索界面主题: SearchView

<intent-filter>不需要<category>使用默认值(你通常看到<activity>的元素),因为该系统提供的ACTION_SEARCH明确意图的搜索活动,使用它的组件名称.

然后,你的清单变成:

<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>  
<meta-data android:name="android.app.searchable"
    android:resource="@xml/my_searchable_layout"/>
Run Code Online (Sandbox Code Playgroud)

"传统上,您的搜索结果应该在ListView中显示,因此您可能希望您的可搜索活动扩展ListActivity" - 仍然来自Docs.所以你SearchActivity可能会:

public class SearchActivity extends ListActivity { }  
Run Code Online (Sandbox Code Playgroud)

"当用户从搜索对话框或搜索小部件执行搜索时,系统会创建一个Intent并将用户查询存储在其中.然后系统启动您声明要处理搜索的活动("可搜索的活动")并传达它的意图".您需要使用Intentin 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);
}  
Run Code Online (Sandbox Code Playgroud)

doMySearch()可以是AsyncTask一个新的Thread..连接到a SQLite DataBase,a SharedPreference,无论如何...... "存储和搜索数据的过程对于您的应用程序来说是独一无二的".话虽这么说,你应该创建一个Adapter在列表中提供结果.

以下是使用asynctask填充列表的简短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);
            }
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

最后,我更喜欢使用SearchView带有AppCompatActionBarSherlock的小部件,它主题的最后描述.自从你提出问题(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;
}  
Run Code Online (Sandbox Code Playgroud)

并执行startActivity()的查询传递给方法SearchActivityonOptionsItemSelected方法.然后,您只需将此搜索项添加到菜单中,如下所示(不要忘记自定义前缀),您可以在添加操作视图中看到:

<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" />
Run Code Online (Sandbox Code Playgroud)

样本文件,您可以检索字典其中包含了演示ListView,并提供连接的完整演示SQLite DataBase通过Adapter.
瞧!我希望你找到解决方案,这篇文章将帮助那些想要相同的人.