Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter for android ListView - Space Character

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

Edit: It's a SimpleAdapter not ArrayAdapter

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

Any ideas? Here's 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());
}
};
like image 731
Chris911 Avatar asked Oct 09 '22 14:10

Chris911


1 Answers

If the filtering functionality from SimpleAdapter doesn't fulfill your filtering requirements, you will need to override the SimpleAdapter and implement your own filtering requirements.

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

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

You can find the link to is blog here including how to use examples, and the code in here.

Regards.

like image 62
Luis Avatar answered Oct 12 '22 11:10

Luis