Filtering A ListView Using An ArrayAdapter Without Overriding GetFilter Method
In this Stackoverflow answer, it is indicated that filtering can be accomplished in a ListView without overriding the getFilter method of the ArrayAdapter and instead implementing
Solution 1:
The following question deals with exactly the same issue that I encountered. This question also gives an example of what the filtering is doing, showing the correct number of items by not showing the correct items in the list.
So it seems that this answer is wrong, despite being upvoted six times. I resolved this by not making use of the getFilter
method of the ArrayAdapter
at all. Rather, I create a new ArrayAdapter
in my TextWatcher
instance, as follows:
private TextWatcher filterTextWatcher = 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) {
if (!s.toString().equals("")) {
List<Title> filteredTitles = new ArrayList<Title>();
for (int i=0; i<titles.size(); i++) {
if (titles.get(i).toString().contains(s)) {
filteredTitles.add(titles.get(i));
}
}
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, filteredTitles);
listView.setAdapter(adapter);
}
else {
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, titles);
listView.setAdapter(adapter);
}
}
};
Note that I also moved the declaration List<Title> titles
out of onCreate
and made it a member variable of my Activity
class so that it is accessible inside the onTextChanged
method of filterTextWatcher
.
Post a Comment for "Filtering A ListView Using An ArrayAdapter Without Overriding GetFilter Method"