android - 在 ActionBar 上创建 "history"到 SearchView

标签 android android-actionbar searchview

我想在我的 SearchView 上有历史记录,我一直在谷歌搜索,我找到的唯一合理(?)教程是 this ,但那只是像 Gingerbread ,而不是 API>14。

然后我找到了这段代码:

String[] columnNames = {"_id","text"};
        MatrixCursor cursor = new MatrixCursor(columnNames);
        String[] array = {"Snääälla", "bla bla bla", "Jävla piss"}; //if strings are in resources
        String[] temp = new String[2];
        int id = 0;
        for(String item : array){
            temp[0] = Integer.toString(id++);
            temp[1] = item;
            cursor.addRow(temp);
        }
        String[] from = {"text"};
        int[] to = {android.R.id.text1};
        CursorAdapter ad = new SimpleCursorAdapter(this.getActivity(), android.R.layout.simple_list_item_1, cursor, from, to);
        mSearchView.setSuggestionsAdapter(ad);

而且该代码只工作了一半,因为它不显示您已经编写的结果,它显示所有项目。

我只是希望它看起来像这样:

Google Play Store screnshot

这是我当前用于添加 SearchView 的代码:

res/menu/menu.xml:

<item android:id="@+id/fragment_searchmenuitem"
      android:icon="@drawable/ic_search_white"
      android:title="@string/menu_search"
      android:showAsAction="collapseActionView|ifRoom"
      android:actionViewClass="android.widget.SearchView" />

主要 Activity .java:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if(!mDrawerLayout.isDrawerOpen(mDrawerList)) {
        inflater.inflate(R.menu.fragment_search, menu);

        mMenuItem = menu.findItem(R.id.fragment_searchmenuitem);
        mSearchView = (SearchView) mMenuItem.getActionView();
        mMenuItem.expandActionView();
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                mMenuItem.collapseActionView();
                searchSupport.SearchForLyrics(s);
                actionBar.setSubtitle("Searcing for: " + s);
                return true;
            }

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

    super.onCreateOptionsMenu(menu, inflater);
}

有人可以给我一些开始的东西吗,老实说,我不知道从哪里开始。因此,我们将不胜感激任何帮助。

最佳答案

本页讨论如何为 SearchView 实现历史记录。

http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html

首先,您必须创建一个内容提供者:

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
    public final static String AUTHORITY = "com.example.MySuggestionProvider";
    public final static int MODE = DATABASE_MODE_QUERIES;

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

然后像这样在您的应用程序 list 中声明内容提供者:

<application>
    <provider android:name=".MySuggestionProvider"
              android:authorities="com.example.MySuggestionProvider" />
    ...
</application>

然后像这样将内容提供者添加到您的可搜索配置中:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint"
    android:searchSuggestAuthority="com.example.MySuggestionProvider"
    android:searchSuggestSelection=" ?" >
</searchable>

您可以随时调用 saveRecentQuery() 来保存查询。以下是您可以在 Activity 的 onCreate 方法中执行此操作的方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent  = getIntent();

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
        suggestions.saveRecentQuery(query, null);
    }
}

要清除搜索历史,您只需调用 SearchRecentSuggestions 的方法 clearHistory(),如下所示:

SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
        HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE);
suggestions.clearHistory();

关于android - 在 ActionBar 上创建 "history"到 SearchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166537/

相关文章:

android - Searchview 使应用程序崩溃

android - 如何将 ObjectAnimator 重置为初始状态?

android - 我们可以在android中为optionsmenu的菜单项提供拖放功能吗

java - 如何使操作栏中的自定义字体立即显示

Android - 在 View 上使用 actionbar 的好处?

java - 使用 SearchView 过滤 Recyclerview 后位置错误

android - 无法将 java.lang.String 类型的对象转换为 com.thesis.joinerapp.Model.Joins 类型

android - 是否有事件可以检测屏幕何时变暗?

java - 如何实现action bar的效果消失/出现? (谷歌现在效果)

android - 如何以编程方式设置工具栏 collapseIcon 颜色