Reputation: 2228
I have one edittext and listview having some values. While typing text in edittext the listview contents will change according to the values typed inside edittext.
Now the problem is, the string is in case sensitive. ie, If the original text is Apparel
, then if we type apparel
or appa
the original text is not displaying.
I want to make the string search case insensitive.
My code is,
private List<SearchList> searchTerms(List<SearchList> search_list, String s) {
List<SearchList> matches = new ArrayList<SearchList>();
for (SearchList search_lists : search_list) {
if (search_lists.search_term.contains(s)) {
matches.add(search_lists);
}
}
return matches;
}
Is there any way to achieve this. I have tried a lot.
Upvotes: 3
Views: 4179
Reputation: 3346
string contains(); function is case sensitive. And from your question, I've noticed that your list contains items with Capital letters as well in it. So apply toLowerCase() to both side would cut it.
if (search_lists.search_term.toLowerCase().contains(s.toLowerCase()))
{
matches.add(search_lists);
}
Hope this helps.
Upvotes: 6
Reputation: 103
Just do as like and compare, Hope it will work :)
EditText editText= (EditText) findViewById(R.id.editText);
String editTextString=editText.getText().toString();
editTextString.equalsIgnoreCase(yourCompare);
Upvotes: 0
Reputation: 34884
Try to lower() a string:
private List<SearchList> searchTerms(List<SearchList> search_list, String s) {
List<SearchList> matches = new ArrayList<SearchList>();
for (SearchList search_lists : search_list) {
if (search_lists.search_term.contains(s.toLowerCase())) {
matches.add(search_lists);
}
}
return matches;
}
Upvotes: 2