Creating Getfilter For Baseadapter For Object Titles?
I'm creating a ListView using CardsUI and I'm planning to create a search using getFilter(). The cards each have a title accessible via getTitle(). Every example of getFilter I've
Solution 1:
I've Implemented this kind of feature in my application.
A brief explanation. Implement your own class that extends Filter, like the next class:
privateclassPlanetFilterextendsFilter {
@Overrideprotected FilterResults performFiltering(CharSequence constraint) {
FilterResultsresults=newFilterResults();
// We implement here the filter logicif (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = planetList;
results.count = planetList.size();
}
else {
// We perform filtering operation
List<Planet> nPlanetList = newArrayList<Planet>();
for (Planet p : planetList) {
if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
nPlanetList.add(p);
}
results.values = nPlanetList;
results.count = nPlanetList.size();
}
return results;
}
}
In your base adapter implements Filterable interface and it has to implement getFilter() method:
@OverridepublicFiltergetFilter() {
if (planetFilter == null)
planetFilter = newPlanetFilter();
return planetFilter;
}
And to tie all together, use textWatcher
on your edittext
, where you enter the text.
editTxt.addTextChangedListener(newTextWatcher() {
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"]");
aAdpt.getFilter().filter(s.toString());
}
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@OverridepublicvoidafterTextChanged(Editable s) {
}
});
That code is taken from the next tutorial.
Post a Comment for "Creating Getfilter For Baseadapter For Object Titles?"