SearchView is not filtering the ListView items with spaces

I have got a SearchView and a ListView. I'm using the following code to filter the ListView items in my onCreate method

 ListAdapter listAdapter = new ListAdapter(this, ChainageFactory.getAllListViewItems());
 listView.setAdapter(listAdapter);
 listView.setTextFilterEnabled(true);
 filter = listAdapter.getFilter();
 searchView.setIconifiedByDefault(false);
 searchView.setOnQueryTextListener(this);
 searchView.setSubmitButtonEnabled(true);
 searchView.setQueryHint("Search Here");

Following are my onQueryTextChange and onQueryTextSubmit method.

@Override
    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            filter.filter(null);
        } else {
            filter.filter(newText);
        }
        return true;
    }

    public boolean onQueryTextSubmit(String query) {
        return false;
    }

My ListView consists of following items.

  1. DIK70300-DI K7 300 mm Dia
  2. DIK70300-DI K7 400 mm Dia
  3. AIK70300-DI D7 450 mm Dia
  4. AIK70300-DI D7 500 mm Dia

Im able filter the ListView items if the user types DI or AI but in case if the user types D7 followed by space of if the user types K7 followed by space, it is not filtering the ListView items though there are items with those characters. How can I be able to sort this out?

Answers


Perform filter on your actual List not on listAdapter

@Override
public boolean onQueryTextChange(String newText) {

    for(int i = 0; i < ChainageFactory.getAllListViewItems().size(); i++){
        if (TextUtils.isEmpty(newText)) {
            filter.filter(null);
        } else {
            String itemName = ChainageFactory.getAllListViewItems().YourItem();

            if(itemName.contains(newText)){
                //Perform your operation with filtered data
            }
        }
    }
    return true;
}

public boolean onQueryTextSubmit(String query) {
    return false;
}

Tyring to put .trim() in into your function. For me it worked well.


Need Your Help