ActionBarSherlock 4.2 是否支持 SearchView 的搜索建议?

2024-01-25

一个月前,我将 ActionBarSherlock 4.2 添加到我的项目中。除了我的搜索建议之外,一切都正常工作了SearchView。我创建搜索建议的方式是使用Android文档中的方法 http://developer.android.com/guide/topics/search/adding-custom-suggestions.html.

ActionBarSherlock 支持搜索建议吗?我试图挖掘问题 https://github.com/JakeWharton/ActionBarSherlock/issues/659列表在 Github 页面上,但问题似乎已经结束,但我似乎无法关注讨论并了解它是否真的已解决。我认为使用过 ActionBarSherlock 的人可能更了解。


事实并非如此。但我找到了一种方法让它查询您的 ContentProvider。 我查看了执行查询的 API 17 中 SuggestionsAdapter 的源代码,并得到了替换此方法的想法。我还发现 ActionbarSherlock 的 SuggestionsAdapter 不使用您的 SearchableInfo。

在 ActionBarSherlock 项目中编辑 com.actionbarsherlock.widget.SuggestionsAdapter:

添加一行

private SearchableInfo searchable;

在构造函数中,添加

this.searchable = mSearchable;

将 getSuggestions 方法替换为以下方法:

public Cursor getSuggestions(String query, int limit) {

    if (searchable == null) {
        return null;
    }

    String authority = searchable.getSuggestAuthority();
    if (authority == null) {
        return null;
    }

    Uri.Builder uriBuilder = new Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(authority)
            .query("")  // TODO: Remove, workaround for a bug in Uri.writeToParcel()
            .fragment("");  // TODO: Remove, workaround for a bug in Uri.writeToParcel()

    // if content path provided, insert it now
    final String contentPath = searchable.getSuggestPath();
    if (contentPath != null) {
        uriBuilder.appendEncodedPath(contentPath);
    }

    // append standard suggestion query path
    uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);

    // get the query selection, may be null
    String selection = searchable.getSuggestSelection();
    // inject query, either as selection args or inline
    String[] selArgs = null;
    if (selection != null) {    // use selection if provided
        selArgs = new String[] { query };
    } else {                    // no selection, use REST pattern
        uriBuilder.appendPath(query);
    }

    if (limit > 0) {
        uriBuilder.appendQueryParameter("limit", String.valueOf(limit));
    }

    Uri uri = uriBuilder.build();

    // finally, make the query
    return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
}

现在它查询我的ContentProvider,但默认适配器崩溃,说没有layout_height从支持库加载一些xml文件。所以你必须使用自定义的SuggestionsAdapter。这对我有用:

import com.actionbarsherlock.widget.SearchView;

import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public final class DrugsSearchAdapter extends CursorAdapter
{
    private static final int QUERY_LIMIT = 50;

    private LayoutInflater inflater;
    private SearchView searchView;
    private SearchableInfo searchable;

    public DrugsSearchAdapter(Context context, SearchableInfo info, SearchView searchView)
    {
        super(context, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        this.searchable = info;
        this.searchView = searchView;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View v, Context context, Cursor c)
    {
        String name = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
        TextView namet = (TextView) v.findViewById(R.id.list_item_drug_name);
        namet.setText(name);

        String man = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
        TextView manuf = (TextView) v.findViewById(R.id.list_item_drug_manufacturer);
        manuf.setText(man);
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2)
    {
        return this.inflater.inflate(R.layout.list_item_drug_search, null);
    }

    /**
     * Use the search suggestions provider to obtain a live cursor.  This will be called
     * in a worker thread, so it's OK if the query is slow (e.g. round trip for suggestions).
     * The results will be processed in the UI thread and changeCursor() will be called.
     */
    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        String query = (constraint == null) ? "" : constraint.toString();
        /**
         * for in app search we show the progress spinner until the cursor is returned with
         * the results.
         */
        Cursor cursor = null;
        if (searchView.getVisibility() != View.VISIBLE
                || searchView.getWindowVisibility() != View.VISIBLE) {
            return null;
        }
        try {
            cursor = getSuggestions(searchable, query, QUERY_LIMIT);
            // trigger fill window so the spinner stays up until the results are copied over and
            // closer to being ready
            if (cursor != null) {
                cursor.getCount();
                return cursor;
            }
        } catch (RuntimeException e) {
        }
        // If cursor is null or an exception was thrown, stop the spinner and return null.
        // changeCursor doesn't get called if cursor is null
        return null;
    }

    public Cursor getSuggestions(SearchableInfo searchable, String query, int limit) {

        if (searchable == null) {
            return null;
        }

        String authority = searchable.getSuggestAuthority();
        if (authority == null) {
            return null;
        }

        Uri.Builder uriBuilder = new Uri.Builder()
                .scheme(ContentResolver.SCHEME_CONTENT)
                .authority(authority)
                .query("") 
                .fragment(""); 

        // if content path provided, insert it now
        final String contentPath = searchable.getSuggestPath();
        if (contentPath != null) {
            uriBuilder.appendEncodedPath(contentPath);
        }

        // append standard suggestion query path
        uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);

        // get the query selection, may be null
        String selection = searchable.getSuggestSelection();
        // inject query, either as selection args or inline
        String[] selArgs = null;
        if (selection != null) {    // use selection if provided
            selArgs = new String[] { query };
        } else {                    // no selection, use REST pattern
            uriBuilder.appendPath(query);
        }

        if (limit > 0) {
            uriBuilder.appendQueryParameter("limit", String.valueOf(limit));
        }

        Uri uri = uriBuilder.build();

        // finally, make the query
        return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
    }

}

并在SearchView中设置这个适配器

searchView.setSuggestionsAdapter(new DrugsSearchAdapter(this, searchManager.getSearchableInfo(getComponentName()), searchView));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ActionBarSherlock 4.2 是否支持 SearchView 的搜索建议? 的相关文章

随机推荐

  • 查找是否有一个元素重复n/k次

    你有一个数组大小n and a constant k 任何 您可以假设数组是 int 类型 尽管它可以是任何类型 描述一种算法 用于查找是否存在至少重复自身的元素n k次 如果有返回一次 在线性时间内执行此操作 O n 要点 使用常量内存执
  • 使用“instanceof function() {}”背后的原因?

    在 Mozilla 开发者中心 有一个关于Function prototype bind https developer mozilla org en JavaScript Reference Global Objects Function
  • 使用 MVC 渲染带有嵌入 Razor 变量的动态 HTML

    我有一些编码的 Html 其中嵌入了我已存储并需要检索的任意数量的 1000 个不同的 Razor 变量从数据库中 我希望能够在 MVC razor 视图中渲染它 只是保存在数据库中的 html 的一个简单示例 可能更复杂 span You
  • 如何通过 FileZilla 访问 Google Kubernetes Engine FTP 服务器

    我创建了一个 gcePreviousDisk 并创建了一个集群并挂载它 这是yaml文件 参考https github com aledv kubernetes ftp https github com aledv kubernetes f
  • SubscribeOn 和 ObserveOn 有什么区别

    我刚刚发现SubscribeOn 这让我想知道我是否应该使用它而不是ObserveOn 谷歌带我去here http social msdn microsoft com Forums en US rx thread 6944f097 00f
  • 如何在观察者中处理具有不同状态值类型的 Observables

    首先是上下文和问题 框架代码在帖子底部 我们正在创建并实现一个 C 框架 以便在 Arduino 等环境中使用 为此 我想使用观察者模式 其中任何对传感器状态变化感兴趣的组件 Observables 可以注册自己 并且它将通过 Observ
  • Qt modbus串口流控处理

    我正在通过串行端口使用 QModbusDevice 编写一个小程序 使用QModbusRtuSerialMaster类 并有一些问题 问题之一似乎是串口的流量控制不正确 检查串行端口嗅探器时 我发现工作客户端在发送请求时打开 RTS 然后关
  • 为什么 mySet.erase(it++) 不是未定义的行为,或者确实如此?

    根据对于这个得到高度评价的答案 https stackoverflow com questions 2874441 deleting elements from stl set while iterating 2874533 2874533
  • 方向改变时替换布局

    我的应用程序有一个 webview 和 LinearLayout 内的一些按钮 问题是 我希望按钮在纵向模式下位于底部 在横向模式下位于左侧 同时 web 视图保持其状态 两种不同的布局不起作用 因为它会强制重新创建刷新 Web 视图的活动
  • 用诗歌管理 git 子模块的依赖关系

    我们有一个存储库app lib它在其他 4 个存储库中用作子模块 并且在每个存储库中我都必须添加子模块的所有依赖项 因此 如果我添加 删除依赖项app lib我必须调整所有其他存储库 我有办法告诉 Poetry 安装根存储库依赖项和子模块中
  • 使用 jQuery 选择 id 中带有百分号 (%) 的元素

    我有一个这样的元素 a href hello a 我一直在拼命尝试用 jQuery 选择它 但不能 我试过了 a my id obviously won t work a my id no such luck a my 20id unrec
  • MYSQL触发器更新复制整行

    我正在尝试创建一个触发器来将整行复制到任何UPDATE 我有2张桌子 Frequencies and Frequencies Audit 这是我的触发器 create trigger auditlog before update on fr
  • 通过 VueJS 2 重用模态

    我在 JSFiddle 使用 Vue 版本 1 中看到了重用模式 https jsfiddle net kemar d3jecL8n https jsfiddle net kemar d3jecL8n 但是当我换成Vue 2版本时 就不行了
  • GNUPLOT:从平滑累积中保存数据

    我绘制了实数均匀随机分布 n 1000 的简单累积和直方图 http www filedropper com random1 1 http www filedropper com random1 1 随机1天 宏是 unset key cl
  • React 组件是否会深入比较 props 以检查是否需要重新渲染?

    我想知道 React 组件是否扩展React Component在尝试决定是否需要重新渲染时对对象进行深入比较 例如 给定 const Foo bar gt return div bar baz div class App extends
  • Titanium Desktop 中的 SVG?

    我正在运行 Titanium Desktop 1 1 0 SDK 并且只有我的 SVG 文本元素可以正确呈现 SVG 方法 例如 getBBox 会给出错误消息 该应用程序在 Titanium 环境之外运行良好 即 Chrome Firef
  • 支持可选参数和.Net 4.0的C#模拟框架

    是否存在完全支持 Net 4 0 和 C 的 C 模拟框架 具体来说 我正在寻找它支持可选参数 我能够通过编写一个扩展方法来让 Moq 来处理这个问题 该方法接受方法名称以及参数名称和值的字典 did想要指定 并且扩展方法为您未指定的所有参
  • Docker Compose + Rails:迁移的最佳实践?

    我刚刚关注了这篇文章在 Docker 中运行 Rails 开发环境 https blog codeship com running rails development environment docker 好文章 效果很好 设置完所有内容后
  • 离子启动给出错误:生成的命令有错误:npminstall

    我正在尝试使用 ionic 和 cordova 创建一个移动应用程序 但是当我启动命令时 ionic start appname blank 下载后 npm 给我 Error with start undefined Error Initi
  • ActionBarSherlock 4.2 是否支持 SearchView 的搜索建议?

    一个月前 我将 ActionBarSherlock 4 2 添加到我的项目中 除了我的搜索建议之外 一切都正常工作了SearchView 我创建搜索建议的方式是使用Android文档中的方法 http developer android c