Start new activity from SearchView

前端 未结 3 1189
清酒与你
清酒与你 2020-12-13 02:53

I have 2 activities: the first has a action bar with a search view, the second should display the results of the search query.

androidmanifest:

    &         


        
3条回答
  •  庸人自扰
    2020-12-13 03:21

    Without seeing your activity code, I would suggest you try this approach - also assuming you have all the files created as stated above;

    In your results activity,

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.core_actions, 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.setQueryHint(getString(R.string.search_hint));
    
        searchView.setOnQueryTextListener(this);
    
        return true;
    }
    

    Remember that this is the activity that has your data that you want to make searchable:

    You must implement the SearchView.OnQueryTextListener interface in the same activity:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                ProductsResulstActivity.this.finish();
    
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
    
    @Override
    public boolean onQueryTextChange(String newText) {
    
        productFilterAdapter.getFilter().filter(newText);
    
        if (TextUtils.isEmpty(newText)) {
             listView.clearTextFilter();
         }
        else {
           listView.setFilterText(newText);
        }
    
        return true;
    }
    

    productFilterAdapter is the adapter that you must create beforehand.

    It should implement Filterable interface. I hope this helps.

    If you need further assistance, please let me know. Good luck

提交回复
热议问题