2013-03-12 58 views
1

我已經能夠成功實現搜索小部件體驗,以使用返回帶有行數據的遊標的內容提供程序。按照預期,自定義搜索建議很好地顯示在ActionBar搜索框下的列表中。如何將選定的搜索建議數據發送到自定義活動

我需要做的是將選定的搜索建議發送到自定義活動(大概是在一個捆綁?)它似乎很簡單,但我一直無法弄清楚。

目前,這段代碼會問我想用什麼應用程序來打開這個意圖。我想將選定的建議數據發送到下面進一步列出的清單中的「MainActivity」。

在此先感謝!

searchable.xml

<?xml version="1.0" encoding="utf-8"?> 

<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:label="@string/app_name" 
    android:hint="@string/search_hint" 
    android:searchSuggestAuthority="com.myapp.SearchProvider" 
    android:searchSuggestIntentAction="android.intent.action.VIEW" 
    android:searchSuggestThreshold="2" 
    android:searchMode="queryRewriteFromText" > 


</searchable> 

搜索活動

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


     Intent intent = getIntent(); 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      // Handle the normal search query case 
      android.util.Log.w("****", "in ACTION_SEARCH"); 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      doSearch(query); 
     } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
      // Handle a suggestions click (because the suggestions all use ACTION_VIEW) 
      android.util.Log.w("****", "in ACTION_VIEW"); 
      doView(intent); 
     } 
    } 

    private void doSearch(String query) { 

     android.util.Log.w("search query:", query); 

    } 

    private void doView(final Intent queryIntent) { 
     Uri uri = queryIntent.getData(); 
     String action = queryIntent.getAction(); 
     Intent i = new Intent(action); 
     i.setData(uri); 
     startActivity(i); 
     this.finish(); 
    } 
} 

清單的搜索部分:

<activity 
     android:name="com.myapp.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

     <activity android:name="com.myapp.SearchableActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
      </intent-filter> 
      <meta-data android:name="android.app.searchable" 
         android:resource="@xml/searchable"/> 
     </activity> 

     <provider android:authorities="com.myapp.SearchProvider" 
      android:name="com.myapp.SearchProvider" /> 

     <meta-data android:name="android.app.default_searchable" 
      android:value="com.myapp.SearchableActivity" /> 

回答

4

呼,我想通了。

在我的內容提供商中,我必須在Matrix光標中添加一個名爲SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA 的列,並放入建議的值。

在我的搜索活動doView()方法我可以提取它:

Bundle extras = queryIntent.getExtras(); 
String data = extras.getString(SearchManager.EXTRA_DATA_KEY); 
android.util.Log.w("keySet =", extras.keySet().toString()); // this showed me the keys available 
android.util.Log.w(SearchManager.EXTRA_DATA_KEY, data); 

從這裏,我可以傳遞到我的自定義活動/意圖。有可能有更好的方法,但這是有效的!

相關問題