【问题标题】:How can I add a searchview to my Android app?如何将搜索视图添加到我的 Android 应用程序?
【发布时间】:2011-06-02 11:09:47
【问题描述】:

我想在我的 Android 应用中添加一个搜索视图,但我看不懂文档。我已经添加了

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
       android:includeInGlobalSearch="true"
       android:searchSuggestAuthority="dictionary"
       android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>

到我的 xml 和 &lt;intent-filter&gt;

<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" />

到我的清单。但是provider-tag 应该去哪里呢?运行应用程序时出现错误膨胀类异常。 有谁知道好的教程吗?谢谢!

【问题讨论】:

    标签: android search


    【解决方案1】:

    这个答案很晚,但正如我所见,其他答案只是 answer-links。然后,我将尝试用简短的示例代码提供一些解释。
    Documentation 中描述了添加SearchView,并且很容易按照这些步骤进行操作。正如我们在Create a Search Interface 主题上看到的那样:

    &lt;intent-filter&gt;不需要带有 DEFAULT 值的&lt;category&gt;(您通常在&lt;activity&gt;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 小部件与AppCompatActionBarSherlock 一起使用,它描述了主题的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 的完整演示。
    瞧!我希望你找到了解决方案,这篇文章将帮助那些想要同样的人。

    【讨论】:

    • 请停止发布不正确/不完整的 Android 文档链接。它们太多了,链接到通用样本根本没有帮助。就像指向http://developers.android.com 一样糟糕。最重要的是,我可以想到很多人对他们的示例代码布局感到困惑。这一直在变化。
    • 亲爱的@Sonny,我同意最后的示例文档,但我目前没有找到可搜索字典(我稍后会更改)。但是,“不正确/不完整” 链接?哼,对不起,但我不这么认为。正如我所说,这个答案是:“用简短的示例代码提供一些解释” - 不是完整的 Android 文档!这些链接与我写的内容相关。你觉得不好吗?然后,编辑它并提出你的建议!
    • 请接受我的道歉。我将通过编辑提出我的建议——也就是说,现在 android 站点中没有这样的示例。我在试图让我的搜索工作(2 天以上,而且还在继续)时抓住了你的答案,我很抱歉我很粗鲁。也就是说,我会说 Android 文档在某些方面非常好,但在某些方面非常杂乱无章。 (例如,初级开发人员应该关注 API 指南还是培训?或两者兼而有之?它们会引导您走不同的道路。两者之间有什么区别?文档是否已更新以实现错误?例如,Date/TimePickerDialog。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多