【问题标题】:How to send selected search suggestion data to a custom activity如何将选定的搜索建议数据发送到自定义活动
【发布时间】:2013-03-12 02:58:10
【问题描述】:

我已经能够成功实现搜索小部件体验,以使用返回带有行数据的游标的内容提供程序。自定义搜索建议按预期很好地显示在 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" />

【问题讨论】:

    标签: android search android-actionbar autosuggest


    【解决方案1】:

    呼,我想通了。

    在我的内容提供程序中,我必须在矩阵光标中添加一个名为: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);
    

    从这里,我可以将其传递给我的自定义活动/意图。可能有更好的方法,但这可行!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      相关资源
      最近更新 更多