是否ActionBarSherlock 4.2支持搜索建议的搜索查看? [英] Does ActionBarSherlock 4.2 support search suggestions for a SearchView?

查看:124
本文介绍了是否ActionBarSherlock 4.2支持搜索建议的搜索查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个月前,我放弃了,在ActionBarSherlock 4.2到我的项目。我得到的一切工作,除了我的搜索查看的搜索建议。我创建使用在Android文档方法。

A month ago, I dropped-in ActionBarSherlock 4.2 into my project. I got everything to work, except the search suggestions for my SearchView. The way I was creating search suggestions was using the method in the Android documentation.

是否ActionBarSherlock支持搜索建议?我试图通过 GitHub的页面上的问题名单,但这个问题似乎关闭,但我似乎无法按照讨论并了解它是否真的是解决与否。我认为,一些你一直在使用ActionBarSherlock谁可能更清楚。

Does ActionBarSherlock support search suggestions? I tried to dig through the issue list on the Github page but the issue seems closed but I can't seem to follow the discussion and understand whether it really is a resolved or not. I thought that some of you who've been using ActionBarSherlock might know better.

推荐答案

它没有。但我已经找到了一种方法,使其查询您的ContentProvider。 我看着SuggestionsAdapter从17 API执行查询所在的来源,取代了这种方法的想法。此外,我发现ActionbarSherlock的SuggestionsAdapter不使用你的SearchableInfo。

It doesn't. But I have found a way to make it query your ContentProvider. I looked into the source of SuggestionsAdapter from API 17 where the query executes and got an idea of replacing this method. Also I found that ActionbarSherlock's SuggestionsAdapter does not use your SearchableInfo.

在您A​​ctionBarSherlock项目编辑com.actionbarsherlock.widget.SuggestionsAdapter:

Edit com.actionbarsherlock.widget.SuggestionsAdapter in your ActionBarSherlock project:

添加一行

private SearchableInfo searchable;

在构造函数中添加

this.searchable = mSearchable;

替换getSuggestions方法与这一个:

Replace getSuggestions method with this one:

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。这是为我工作:

Now it queries my ContentProvider but crashes with default adapter saying that no layout_height loading some xml file from support library. So you have to use custom SuggestionsAdapter. This is what worked for me:

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);
    }

}

和在搜索查看设置此适配器

And set this adapter in SearchView

searchView.setSuggestionsAdapter(new DrugsSearchAdapter(this, searchManager.getSearchableInfo(getComponentName()), searchView));

这篇关于是否ActionBarSherlock 4.2支持搜索建议的搜索查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆