android pass bundle with search

后端 未结 5 1826
南笙
南笙 2020-12-06 13:02

Hello i am using android 3.0 search widget(not the search mode), the problem is i need to pass some parameters along with the search word.

Android i

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 13:21

    you can use the follow

    in Menu

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_al_ayats, menu);
    
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        Bundle appData = new Bundle();
        appData.putString("hello", "world");
    
        searchView.setAppSearchData(appData);
    
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
    
        return true;
    }
    

    and in your search result activity put the below

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_search_result);
    
        handleIntent(getIntent());
    }
    
        private void handleIntent(Intent intent) {
    
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String extrastring=intent.getBundleExtra(SearchManager.APP_DATA).getString("hello");
            //use the query to search your data somehow
            getSupportActionBar().setTitle(extrastring);
        }
    }
    

提交回复
热议问题