【问题标题】:Android: searchview history not showingAndroid:搜索视图历史不显示
【发布时间】:2015-01-06 09:34:47
【问题描述】:

我正在尝试获取一个搜索视图,以在用户开始输入时显示以前输入的搜索。

我已按照http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html 的所有说明进行操作,并查看了其他各个地方,但似乎无法正常工作。

我在我的应用程序中使用了一个片段的搜索视图,它允许用户查找特定站点的公交时刻表。代码如下。

transit.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
          android:title="Search"
          android:icon="@android:drawable/ic_menu_search"
          app:showAsAction="always"
          app:actionViewClass="android.support.v7.widget.SearchView"
          android:searchSuggestAuthority="com.teamzeta.sfu.utilities.BusSuggestionProvider"
          android:searchSuggestSelection=" ?"/>
</menu>

BusSuggestionProvider

package com.teamzeta.sfu.utilities;

import android.content.SearchRecentSuggestionsProvider;

public class BusSuggestionProvider extends SearchRecentSuggestionsProvider{
    public final static String AUTHORITY = "com.teamzeta.sfu.utilities.BusSuggestionProvider";
    public final static int MODE = DATABASE_MODE_QUERIES;

    public BusSuggestionProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
}

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/stop_number"
    android:searchSuggestAuthority="com.teamzeta.sfu.utilities.BusSuggestionProvider"
    android:searchSuggestSelection=" ?">
</searchable>

NextBusFragment onCreateOptionsMenu

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.transit, menu);
        MenuItem searchViewItem = menu.findItem(R.id.action_search); 
        SearchView searchView = (SearchView) searchViewItem.getActionView();
        searchView.setIconifiedByDefault(false);
        searchView.setInputType(InputType.TYPE_CLASS_NUMBER);
        searchView.setQueryHint(getResources().getString(R.string.stop_number));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                search(s);
                SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(), BusSuggestionProvider.AUTHORITY, BusSuggestionProvider.MODE);
                suggestions.saveRecentQuery(s, null);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
    }

清单提供者

 <provider android:name="com.teamzeta.sfu.utilities.BusSuggestionProvider"
               android:authorities="com.teamzeta.sfu.utilities.BusSuggestionProvider" />
 <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>

search() 方法启动并从 API 检索总线列表,然后以列表视图的形式将其显示在屏幕上。

谁能弄清楚为什么这没有显示历史?

非常感谢!

【问题讨论】:

  • 您在onQueryTextSubmit 中调试过吗? s 的值是否合适?
  • 提交的值不错。
  • 2020年遇到同样的问题

标签: android history searchview


【解决方案1】:

您需要在清单中将您的活动设置为“可搜索”:

<activity>
...
<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多