Search code examples
androidsearchview

How to separate SearchView items to filter a RecyclerView


Currently, I'm working on an activity that has a RecyclerView and SearchView in it.Now the RecyclerView and SearchView are working fine but there is a problem that I don't know how to resolve, you see in the searchView part we were a task to make a SearchView that accepts 2 or more searchItems that must be separated by a comma or space in order to filter the recyclerview again if the second searchitem matches an item in the RecyclerView, for example I type in searchitem "Big" i'll need to separate it again with another searchitem like "Black"

here is the code for the SearchView

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pet_book_main);

        if(savedInstanceState != null){
           mSearchString=savedInstanceState.getString(SEARCH_KEY);
        }

        pet_title=(TextView)findViewById(R.id.pet_book_title);
        petbook=(RecyclerView)findViewById(R.id.pet_book_pet);
        navbot=(BottomNavigationView) findViewById(R.id.petbook_navbot);
        toolbar=(Toolbar)findViewById(R.id.petbook_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setTitle("");
        linearLayoutManager=new LinearLayoutManager(this);
        petbook.setHasFixedSize(true);
        petbook.setLayoutManager(linearLayoutManager);
        refresh();
        disableShiftMode(navbot);
}

     @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater menuInflater= getMenuInflater();
        menuInflater.inflate(R.menu.petbook_searck,menu);
        searchItem=menu.findItem(R.id.pet_book_search_mode);
        searchView =(SearchView)MenuItemCompat.getActionView(searchItem);

        if (mSearchString != null && !mSearchString.isEmpty()) {
            searchItem.expandActionView();
            searchView.setQuery(mSearchString, true);
            searchView.clearFocus();
        }
        search(searchView);
        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                pet_title.setVisibility(View.INVISIBLE);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                pet_title.setVisibility(View.VISIBLE);
                return true;
            }
        });
        return true;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mSearchString = searchView.getQuery().toString();
        outState.putString(SEARCH_KEY, mSearchString);
    }

      public void search(android.support.v7.widget.SearchView search)
    {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                newText=newText.toLowerCase();
                newList=new ArrayList<>();
                for(petbook_getItem items : bookItem){
                    String pets=items.getPet_name().toLowerCase();
                    if(pets.contains(newText))
                        newList.add(items);
                }
                adapter.filterSearch(newList);
                adapter.notifyDataSetChanged();
                return true;
            }
        });
    }

Solution

  • This code handles user input in search which can be multiple comma separated search items:

    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        .....
    
    
        @Override
        public boolean onQueryTextChange(String newText) {
            newText = newText.toLowerCase();
    
            // remove leading comma or space
            String string2 = newText.replaceAll("^[,\\s]+", "");
    
            // return early for invalid input (empty)
            if (string2.isEmpty()) {
                return true;
            }
    
            // split by comma & space if any
            List<String> inputList = Arrays.asList(string2.split("[,\\s]+"));
    
            newList = new ArrayList<>();
    
            for (petbook_getItem items : bookItem){
                String pets = items.getPet_name().toLowerCase();
    
                // Check for matching by looping through each search items
                for (String inputTxt : inputList) {
                    if (pets.contains(inputTxt)) {
                        newList.add(items);    
                        break;
                    }
                }
            }
    
            adapter.filterSearch(newList);
            adapter.notifyDataSetChanged();
            return true;
        }
    });