Filter for android ListView - Space symbol

I want to filter my listView using the EditText window and use the adapter function getFilter() . It works fine until I put a space in the text box.

Edit: this is a SimpleAdapter not ArrayAdapter

If my list contains the following words: {"Apple", "Banana", "Red Apple"} If I type "apple", it will return all the items containing the word apple (Apple and Red Apple) in it. If I type "apple", it will not return anything.

Any ideas? Here is the code:

 searchBox = (EditText) findViewById(R.id.searchBox); searchBox.addTextChangedListener(filterTextWatcher); 

and

 private TextWatcher filterTextWatcher = new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void afterTextChanged(Editable s) { // TODO Auto-generated method stub myAdapter.getFilter().filter(s.toString()); } }; 
+5
source share
2 answers

If the filtering function from the SimpleAdapter does not meet your filtering requirements, you need to override the SimpleAdapter and implement your own filtering requirements.

This requires a lot of code, since Filter in SimpleAdapter uses a lot of private objects that you will need to replicate. The same aplies for the ArrayAdapter

Fortunatly, someone else (@ uʍop ǝpısdn) already went into the process, wrote the code and made it available to the community.

You can find the blog link here , including the use of examples and code.

Sincerely.

+2
source

Try using this:

 myAdapter.getFilter().filter(s.toString().trim()); 
+2
source

Source: https://habr.com/ru/post/901664/


All Articles