2012-12-10 8 views
5

1ヶ月前、私はActionBarSherlock 4.2をプロジェクトにドロップインしました。私はSearchViewのための検索提案を除いて、働くためにすべてを得ました。検索提案を作成する方法はthe method in the Android documentationを使用していました。ActionBarSherlock 4.2はSearchViewの検索候補をサポートしていますか?

ActionBarSherlockは検索候補をサポートしていますか?私はGithubページのthe issueリストを掘り下げようとしましたが、問題はクローズしているようですが、ディスカッションに従うことができず、本当に解決済みかどうかを理解できません。私は、ActionBarSherlockを使ってきたあなたの中には、よく知っている人もいると思っていました。

答えて

12

これはありません。しかし、あなたのContentProviderに問い合わせる方法を見つけました。 私は、クエリが実行され、このメソッドを置き換えるアイディアを得たAPI 17からSuggestionsAdapterのソースを調べました。また、ActionbarSherlockのSuggestionsAdapterがSearchableInfoを使用していないことがわかりました。あなたのActionBarSherlockプロジェクトで

編集com.actionbarsherlock.widget.SuggestionsAdapter:

はこの1つでgetSuggestions方法を交換し

this.searchable = mSearchable; 

を追加し、コンストラクタで

private SearchableInfo searchable; 

の行を追加します。

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に照会しましたが、デフォルトのアダプターでクラッシュして、サポートライブラリから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); 
    } 

} 

そして、私はこのためにthe github issueを開いた一人ですSearchView

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

この偉大な答えを書くために時間を割いてくれてありがとう: –

2

にこのアダプタを設定します。 devブランチに取り組んでいます。現在のバージョン(4.2)には修正がありません。このcommitによって完全に修正されましたが、私はdevブランチをチェックアウトして試してみることをお勧めします。

+0

dev-branchは現在マスターに合併されているようですが、修正コミットはマスターもマスターを使用する必要があります。 –

0

私がここで間違っているか、何かを変更したことがわかりませんが、上記の答えはうまくいかず、ActionBarSherlock SuggestionsAdapterは機能しません。私が得るのは、runQueryOnBackgroundThreadのヌルポインタです。 bindViewなどには決して行きませんが、提案結果を表示することはできます。私はandroid.app.SearchManagerは何とかABSをgetSuggestions()でオーバーライドしているとは思いますが、わかりません。私はまだ物事を試しています...

関連する問題