Android Custom Listview: Adding Search Function
i've a problem, i want an EditText above my ListView that performs a search in that ListView. Is it possible to do? This is the Activity code: public static class BirreChiareListVi
Solution 1:
Yes this is definitely possible to do. Android provides a Filterable
interface for ArrayAdapter
s. The interface provides a getFilter()
method where you can hook into the filtering process.
When the text changes in your EditText
, you call adapter.getFilter().filter("filterString")
. The text changing behaviour for the EditText
is done by using a TextWatcher
on the EditText
. This following tutorial, explains it in good detail:
http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html
EDIT:
To implement the filterable interface, just do this in your ArrayAdapter
:
staticclassMyAdapterextendsArrayAdapter<String> implementsFilterable {
privateFilter myFilter;
@OverridepublicFiltergetFilter() {
if (myFilter == null)
myFilter = newMyFilter();
return myFilter;
}
}
where MyFilter
is your own Filter
class.
Post a Comment for "Android Custom Listview: Adding Search Function"