Search code examples
androidlistviewfilteradaptersearchview

SearchView ignore capital letter


I added a searchview with listview to my application-project.

I have the problem, that the searchview ignores all capital letter so for example:

When I type "pple" in searchviw, the listview shows the row Apple. But when I type Apple or apple, it doesnt find the Apple. When I change the String from Apple to apple, it works when I type apple but not when I type Appel...

I hope I didn't make it to complicated now :D

I use the following filter code:

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults filterResults = new FilterResults();
                if(constraint == null || constraint.length() == 0){
                    filterResults.count = itemsModelsl.size();
                    filterResults.values = itemsModelsl;

                }else{
                    List<itemsmodel> resultsModel = new ArrayList&lt;>();
                    String searchStr = constraint.toString().toLowerCase();

                    for(ItemsModel itemsModel:itemsModelsl){
                        if(itemsModel.getName().contains(searchStr) || itemsModel.getEmail().contains(searchStr)){
                            resultsModel.add(itemsModel);
                            filterResults.count = resultsModel.size();
                            filterResults.values = resultsModel;
                        }
                    }


                }

                return filterResults;
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                itemsModelListFiltered = (List<itemsmodel>) results.values;
                notifyDataSetChanged();

            }
        };
        return filter;
    }

Please let me know, if you need more code.

Thanks in advance.

Bg Daniel


Solution

  • It's because you lower-cased the search input, but not the item's name itself. So when you type "Apple"/"apple", the search input is actually only the lower-cased version: "apple". On the other hand, the item's name is "Apple" so it cannot be matched.

    Try:

    ...
    if (itemsModel.getName().toLowerCase().contains(searchStr) || itemsModel.getEmail().toLowerCase().contains(searchStr)) {
       ...
    }
    ...