Search code examples
androidlistviewsearchkeywordsearchview

SearchView in ListView search also by "hidden" keywords


I would like to ask you for some pointers where to look for solution of my problem, since googling for past few hours didn't help.

I have a SearchView and ExpandableListView and everything works fine. To each of my listings (Strings) I would like to add keywords that would not be displayed, but would be searchable.

For example if we have a list of fruit:

-Apple
-Banana 
-Orange

...

I would like to assign them hidden keywords like:

-Apple (healthy)
-Banana (tasty)
-Orange (sour)

So when I would type into my SearchView: "healthy" it would only display Apple.

Some of my code (SearchView):

  SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  search = (SearchView) findViewById(R.id.search);
  search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
  search.setIconifiedByDefault(false);
  search.setOnQueryTextListener(this);
  search.setOnCloseListener(this);

and ArrayList:

  ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
  Fruit fruit = new Fruit("Apple", R.drawable.apple);
  fruitList.add(fruit);
  fruit = new Fruit("Orange", R.drawable.orange);
  fruitList.add(fruit);
  fruit = new Fruit("Banana", R.drawable.banana);
  fruitList.add(fruit);

  Basket basket = new Basket("Yes",basketList);
  basketList.add(basket);

  fruitList = new ArrayList<Fruit>();
  Fruit fruit = new Fruit("Peach", R.drawable.peach);
  fruitList.add(fruit);
  fruit = new Fruit("Pineapple", R.drawable.pineapple);
  fruitList.add(fruit);
  fruit = new Fruit("Coconut", R.drawable.coconut);
  fruitList.add(fruit);

  Basket basket = new Basket("No",basketList);
  basketList.add(basket);

  ...

Solution

  • search.setOnQueryTextListener(this);

    You need to show the implementation you have for that. Basically, it's going to be calling this function:

     @Override public boolean onQueryTextSubmit(String query){
        // IMPLEMENT HERE
     }
    

    And that's where you'd handle things. Instead of just checking the query against the name, you'd also check the "hidden" keywords, which would probably be in the Fruit class.