2012-12-10 14 views
5

एक महीने पहले, मैंने अपनी परियोजना में एक्शनबैरशेल 4.2 को छोड़ दिया। मेरे SearchView के लिए खोज सुझावों को छोड़कर, मुझे सबकुछ काम करने के लिए मिला। जिस तरह से मैं खोज सुझाव बना रहा था the method in the Android documentation का उपयोग कर रहा था।क्या ActionBarSherlock 4.2 SearchView के लिए खोज सुझावों का समर्थन करता है?

क्या ActionBarSherlock खोज सुझावों का समर्थन करता है? मैंने गीथब पेज पर the issue सूची के माध्यम से खोदने की कोशिश की लेकिन समस्या बंद हो गई लेकिन मैं चर्चा का पालन नहीं कर सकता और समझ सकता हूं कि यह वास्तव में हल हो गया है या नहीं। मैंने सोचा कि आप में से कुछ जो एक्शनबैरशॉक का उपयोग कर रहे हैं, बेहतर हो सकते हैं।

उत्तर

12

ऐसा नहीं है। लेकिन मुझे इसे आपके ContentProvider से पूछने का एक तरीका मिला है। मैंने एपीआई 17 से सुझाव एडाप्टर के स्रोत में देखा जहां क्वेरी निष्पादित हुई और इस विधि को बदलने का विचार मिला। इसके अलावा मैंने पाया कि एक्शनबार शेरलॉक के सुझाव एडाप्टर आपके 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 से पूछताछ करता है लेकिन डिफ़ॉल्ट एडाप्टर के साथ क्रैश कहता है कि कोई लेआउट_हेइट समर्थन लाइब्रेरी से कुछ XML फ़ाइल लोड नहीं करता है। तो आपको कस्टम सुझाव एडाप्टर का उपयोग करना होगा। यह वही मेरे लिए काम किया है:

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)); 
+0

इस महान उत्तर को लिखने के लिए समय निकालने के लिए बहुत धन्यवाद :) –

2

में इस एडाप्टर सेट मैं एक है कि इस बात के लिए the github issue खोला हूँ। यह देव शाखा पर काम कर रहा है। वर्तमान संस्करण (4.2) में फिक्स नहीं है। यह commit द्वारा पूरी तरह से तय किया गया था, लेकिन मैं सिर्फ देव शाखा की जांच करने और इसे आजमाने का सुझाव दूंगा।

+0

क्योंकि वर्तमान में देव-शाखा मास्टर में विलय हो गई है और इसके आगे, फिक्स-प्रतिबद्ध है मास्टर, मास्टर और मास्टर का उपयोग करना चाहिए। –

0

मुझे नहीं पता कि मैं यहां गलत हूं या मैंने दुर्घटना पर कुछ बदल दिया है, लेकिन उपर्युक्त उत्तर काम नहीं करता है और एक्शनबारशेल सुझाव एडाप्टर काम नहीं करता है। मुझे लगता है कि runQueryOnBackgroundThread में शून्य पॉइंटर्स हैं। यह कभी भी बाइंडव्यू इत्यादि में नहीं जाता है, फिर भी यह सुझाव परिणामों को प्रदर्शित करने का प्रबंधन करता है। मुझे लगता है कि android.app.SearchManager किसी भी तरह से getSuggestions() के साथ एबीएस ओवरराइड कर रहा है लेकिन मुझे यकीन नहीं है। मैं अभी भी चीजों की कोशिश कर रहा हूं ...