如何通过 android SearchView 小部件查询提交的意图开始新的 activity

How start new activity through intent from android SearchView widget query submit

这是我应用程序的主要 activity。从主要 activity 我可以通过从 RecyclerView 列表中选择任何类别导航到项目列表 activity。我还需要通过直接在工具栏的 android SearchView 小部件中搜索项目名称来转到相同的项目列表 activity。我这样写搜索视图的代码

public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.app_bar_item, menu);

        /*final MenuItem item = menu.findItem(R.id.search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);*/

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        if (null != searchView)
        {
            searchView.setSearchableInfo(searchManager
                    .getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(true);
        }

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query)
            {
                Log.d("TAG","You queried for="+query); // Here it prints correct query input. but doesnot starting the new activity.
                Intent intent3 = new Intent(getApplicationContext(),ItemListActivity.class);
                intent3.putExtra("FetchType","ItemSearch");
                intent3.putExtra("CatId",query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        };

        searchView.setOnQueryTextListener(queryTextListener);
        return super.onCreateOptionsMenu(menu);
    }


/*And i accept both the navigations from main activty(By selecting Category list item, or by inputing hint in search widget) in my list item activity like:*/

    Bundle extras=getIntent().getExtras();
        if(extras!=null)
        {
            fetch_hint = extras.getString("FetchHint"); /* Fetch hint is Category name selected or Searchquery input.*/
            fetch_type = extras.getString("FetchType");  /* if activity start from category item selection, Fetch type is set as ByCat, If it is from searchview set as ItemSearch*/
        }

它适用于按类别搜索,但不适用于搜索视图。请帮我。提前致谢

startActivity 未被调用。应用程序上下文无法启动 activity。

Intent intent3 = new Intent(MainActivity.this,ItemListActivity.class);
            intent3.putExtra("FetchType","ItemSearch");
            intent3.putExtra("CatId",query);
startActivity(intent3);

修改您的代码如下:

/*final MenuItem item = menu.findItem(R.id.search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);*/

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    if (null != searchView)
    {
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
    }

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query)
        {
            Log.d("TAG","You queried for="+query); // Here it prints correct query input. but doesnot starting the new activity.
            Intent intent3 = new Intent(getApplicationContext(),ItemListActivity.class);
            intent3.putExtra("FetchType","ItemSearch");
            intent3.putExtra("CatId",query);
            startActivity(intent3);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    };

    searchView.setOnQueryTextListener(queryTextListener);
    return super.onCreateOptionsMenu(menu);
}

替换此代码

         Intent intent3 = new Intent(YourActivityName.this,ItemListActivity.class);
         // instead of getApplicationContext use yourActivityName.this
        intent3.putExtra("FetchType","ItemSearch");
        intent3.putExtra("CatId",query);
        startActivity(intent3); <------------ here is you are missing this      
                                               line