如何通过android SearchView小部件查询提交启动新活动

时间:2016-04-27 07:18:59

标签: android searchview

这是我的应用的主要活动。从主要活动我可以通过从RecyclerView列表中选择任何类别来导航到项目列表活动。此外,我需要通过在工具栏中的android SearchView小部件中直接搜索项目名称来转到相同的项目列表活动。我为searchview写了这样的代码

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*/
        }

通过类别搜索工作正常,但不适用于searchview。请帮我。提前致谢

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

修改您的代码,如下所示:

{{1}}

答案 2 :(得分:0)

替换此代码

         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