Reputation: 248
I'm working with the newest Android SDK and I want to filter my ListView
which represents a list of different plants, with CheckBoxes
.
In my list I have 800 Items with different attributes (e.g. color, size, edibility), and the CheckBoxes
should filter the list in a subtractive way. Only the list items/views which match all attributes should be visible - all other rows should be invisible(the program should work like the filtering system on www.pilzsuchmaschine.de).
I tried to modify the getView()
of my custom ArrayAdapter
but I didn't get the right idea how to do that properly. Does anyone have a solution?
My ArrayAdapter
is pretty the same as this one.
Upvotes: 1
Views: 1586
Reputation: 87064
I tried to modify the getView() of my custom ArrayAdapter but I didn't get the right idea how to do that properly. Does anyone have a solution?
You don't do the filtering in the getView()
method. First set a OnCheckedChangeListener
on all the filter CheckBoxes
to monitor their status(each of those CheckBoxes
should have a boolean
variable to hold its status). When a filter CheckBox
is checked/unchecked update the status variable and then filter the ListView
. Filtering the ListView
can be done in two ways, manually or by using the dedicated mechanism(the Filter
class).
Manually, when the user checks a CheckBox
you'll take all the statuses of the filter CheckBoxes
and match them against each element of the list of plants. Which element matches all the CheckBoxes
statuses is valid and should be added to a new list. After this is done, make the adapter point to the newly created list and call notifyDataSetChanged()
. I wouldn't go with this approach as you have a lot of items.
The proper way would be to make your own adapter along with its Filter
method(in this case the adapter will hold the statuses of the filter CheckBoxes
). When the user checks a filter CheckBox
call a method on the adapter to update the corresponding boolean status. Also call the getFilter()
method on the adapter and do the filtering: ((Filterable) adapter).getFilter().filter(null)
. There are a lot of tutorials there about implementing a Filter
for an adapter.
Upvotes: 1