本文介绍了通过搜索查看/控件调用搜索过程中如何传递额外的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地使用搜索窗口小部件在我的操作栏进行搜索以下本指南。搜索是好的,但我不知道如何在搜索传递额外的变量。同样的指南指出我可以覆盖 onSearchRequested(),但这似乎并不能与搜索小插件工作。

I am successfully using a search widget in my action bar to perform a search following this guide. The search is fine, but I'm wondering how to pass additional variables on a search. The same guide states I can override onSearchRequested(), but this doesn't seem to work with a search widget.

  • 覆盖问题:

  • Override in question:

@Override
public boolean onSearchRequested() {    
    Bundle appData = new Bundle();
    appData.putString("KEY", "VALUE");
    startSearch(null, false, appData, false);
    return true;
}

  • 获取捆在我的活动类:

  • Getting the bundle in my activity class:

    protected void onCreate(Bundle savedInstanceState) {
        // ...
        Intent intent = getIntent();
        Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
        String value = appData.getString("KEY");
        Log.d("VALUE", value);
        // ...
    }
    

  • 在创建搜索类我的应用程序崩溃,因为 APPDATA 总是

    My application crashes upon creating the search class because appData is always null.

    onSearchRequested() 调用,但包不让它给我的的onCreate()方法。

    onSearchRequested() is called, but the bundle does not make it to my onCreate() method.

    所有的传递的意图演员是 {USER_QUERY =我的查询,查询=我的查询}

    All extras from the passed intent are {user_query=my-query, query=my-query}.

    推荐答案

    看来要做到这一点的唯一方法是拦截在您的活动创造了新的活动,是搜索功能。要做到这一点,我们覆盖 startActivity()方法。然后,我们可以检查,以确保该活动的确是搜索活动,然后添加一个额外的意图。工作code是如下。

    It seems the only way to do this is to intercept new activities created in your activity which is search-enabled. To do this we override the startActivity() method. We can then check to make sure the activity is indeed the search activity, then add an extra to the intent. The working code is below.

    @Override
    public void startActivity(Intent intent) {      
        // check if search intent
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            intent.putExtra("KEY", "VALUE");
        }
    
        super.startActivity(intent);
    }
    

    您可以再抢你的额外的,你会使用任何其他多余的在你的搜索活动:

    You can then grab your extra as you would any other extra in your search activity using:

    mValue = intent.getStringExtra("KEY");
    

    这篇关于通过搜索查看/控件调用搜索过程中如何传递额外的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    11-03 05:43