启动SearchView小部件打开应用程序

时间:2016-03-30 19:27:27

标签: android android-layout searchview

您好我需要在searchView中放置一些文本并关注/展开searchView小部件。

这是我尝试的但是它不起作用。只有键盘打开。

是的,我查看了网络上的其他解决方案

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_busqueda, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchView.setQuery("test",true);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();
    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextChange(String newText)
        {
            // this is your adapter that will be filtered

            Log.d("dimar","on text chnge text: " + newText);


            List<Producto> lista = new Select().from(Producto.class).execute();
            List <Producto> lista2 = new ArrayList<Producto>();
            for (int i=0;i<lista.size();i++)
            {
                if (lista.get(i).getNombre().contains(newText)||lista.get(i).getLocal_id().contains(newText))
                {
                 lista2.add(lista.get(i));
                }
            }
            AdaptadorCuentas adap = new AdaptadorCuentas(MainActivity.this,lista2);
            listView.setAdapter(adap);

            return true;
        }
        @Override
        public boolean onQueryTextSubmit(String query)
        {
            // this is your adapter that will be filtered
            Log.d("dimar", "on query submit: " + query);
            return true;
        }
    };
    searchView.setOnQueryTextListener(textChangeListener);



    return super.onCreateOptionsMenu(menu);

}

1 个答案:

答案 0 :(得分:0)

正确的代码

MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchMenuItem.expandActionView();   searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();