I display a friend list in a ListView
and use a TextWatcher
set on an EditText
to allow search. This works with 1 limitation (which I am not getting): typing a character correctly filters the list, removing a char however doesn't do anything. The original list is correctly displayed when the input is empty, but the Filter
is also supposed to work when a character gets removed, since onTextChanged()
gets invoked. Where am I going wrong?
Class (BaseAdapter
) fields:
private ArrayList<User> friends;
private ArrayList<User> originalFriendList;
//to keep track of the original list
this.originalFriendList = friends;
Filter class:
private class FriendSearchFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<User> filterList = new ArrayList<User>();
for (User friend : friends) {
if (StringUtils.containsIgnoreCase(friend.getLast_name(), constraint)
|| StringUtils.containsIgnoreCase(friend.getFirst_name(), constraint)) {
filterList.add(friend);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
// the input is empty
results.count = originalFriendList.size();
results.values = originalFriendList;
}
return results;
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
friends = (ArrayList<User>) results.values;
notifyDataSetChanged();
}
}
TextWatcher
:
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start,
int before, int count) {
/* no action required */
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
if(mFriendListFrag == null || !mFriendListFrag.equals(mFragAdapter.getItem(mViewPager.getCurrentItem())));
mFriendListFrag = (FriendListFragment) mFragAdapter.getItem(mViewPager.getCurrentItem());
mFLAdapter = (FriendListAdapter) mFriendListFrag.getListAdapter();
mFLAdapter.getFilter().filter(charSequence.toString());
Log.e("ON", "TEXT CHANGED");
}
@Override
public void afterTextChanged(Editable editable) {
/* no action required */
}
});
just in case someone has a similar problem: I had to compare the constraint against the original list, and not the "working list" that was overwritten at the time I tried to filter backwards:
for (User friend : originalFriendList)
instead of
for (User friend : friends)