In my application I have create a custom list view and I want to implement a filter so that the list can be filtered according to the text entered in the EditText. I am usin
I find working with filters not all that convenient.. How i do it is:
((EditText)findViewById(R.id.etSearch)).addTextChangedListener(new TextWatcher(){
private boolean mCountIncreased;
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 0){
mDisplayedList.clear();
mDisplayedList.addAll(mFullList);
mListAdapter.notifyDataSetChanged();
return;
}
if (mCountIncreased){
mDisplayedList.clear();
mDisplayedList.addAll(mFullList);
}
List- toRemove = new ArrayList
- ();
for (Item item : mDisplayedList){
if (someCondition)
toRemove.add(currency);
}
}
mDisplayedList.removeAll(toRemove);
mListAdapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mCountIncreased = after <= count;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
Note that you'll have to change your adapter to work with the mDisplayedList instead of the mFullList.. and that's it.
This might give some overhead when your list contains ALOT of entries.. but i've worked like this with a list of +-300 items and i didn't notice anything.
Hope it helps, Vlad