问题
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());
}
};
回答1:
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.
回答2:
Try using this:
myAdapter.getFilter().filter(s.toString().trim());
来源:https://stackoverflow.com/questions/9407428/filter-for-android-listview-space-character