Reputation: 8729
I have a ListView that I filter with and EditText that I have a TextWatcher on to get the text change events. Everything works great until you delete the text out of edit text. The filter then just remains as the first letter and doesn't go back to no filter.
What am I doing wrong? Here is the code for the TextWatcher, I even tried to disable the filter but that had no effect.
EditText txtFilter = (EditText) this.findViewById(R.id.txtFilter);
txtFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length()==0)
{
lv.setTextFilterEnabled(false);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
lv.setTextFilterEnabled(true);
lv.setFilterText(s.toString());
}
});
Thanks for your help
Upvotes: 1
Views: 2218
Reputation: 6797
i think that Adil Soomro answer is bad ... i just look at android source and setFilterText and it looks like
public void setFilterText(String filterText) {
// TODO: Should we check for acceptFilter()?
if (mTextFilterEnabled && !TextUtils.isEmpty(filterText)) {
createTextFilter(false);
// This is going to call our listener onTextChanged, but we might not
// be ready to bring up a window yet
mTextFilter.setText(filterText);
mTextFilter.setSelection(filterText.length());
if (mAdapter instanceof Filterable) {
// if mPopup is non-null, then onTextChanged will do the filtering
if (mPopup == null) {
Filter f = ((Filterable) mAdapter).getFilter();
f.filter(filterText);
}
// Set filtered to true so we will display the filter window when our main
// window is ready
mFiltered = true;
mDataSetObserver.clearSavedState();
}
}
}
so as you can see it's setting adapter with filter ...
you should use lv.clearTextFilter();
instead of lv.setTextFilterEnabled(false);
Upvotes: 2
Reputation: 37729
Instead of filtering ListView
, do filter through Adapter
like this way:
txtFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count){
adapter.getFilter().filter(s);
}
});
Upvotes: 1