Theodore Sternberg
Theodore Sternberg

Reputation: 161

How to obtain results in Filter.Filterlistener of Filter.filter()?

In an Android app, I have a common setup -- a ListView with an ArrayAdapter. At a certain point I call the adapter's getFilter().filter() method, which very nicely restricts the collection of ListView items displayed on the screen. But what I'd really like to do is obtain, programmatically, a list of those displayed items. The Filter object itself seems to "know" this information, but I can't get at that information because Filter.values() is protected. That is, I'd like to do something like this:

Filter myfilter = adapter.getFilter();
myfilter.filter(text, new Filter.FilterListener() {
  @Override
  public void onFilterComplete(int count) {
    Filter.FilterResults results = myfilter.values(); // Won't compile!
    ... do something with results ...
  }
}

Is there a way to get what I want, short of implementing my own subclass of ArrayAdapter? I'm thinking, if Google has gone to the trouble of giving us a count of the number of items that have passed through the filter (that is, through the Filter.FilterListener.onFilterComplete(int count) method), they would have made the items themselves available...somehow, somewhere?

Upvotes: 1

Views: 1364

Answers (1)

Ifrit
Ifrit

Reputation: 6821

When an ArrayAdapter is filtered...the problem is getting the undisplayed items. The displayed items is all you'll ever get to access. So once you conduct a filter, you can simply iterate through the adapter to obtain each item. Pseudocode example:

List<Foo> data = new ArrayList<Foo>();
int count = mAdapter.getCount();
for (int index = 0; index < count; ++index) {
    data.add(mAdapter.getItem(index));
}

This is the generic way to obtain the list of items you've placed into the adapter. If a filter operation never happened, it'll return you all the items in the adapter. If filtering has happened, it'll only return you the displayed data. There are some oddities when working with filtering and the ArrayAdapter. I'd recommend reading this, for further info on it's bugs and gotchas.

Upvotes: 1

Related Questions