Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save williamhqs/361ef70dd7584ff9837a30794f35780a to your computer and use it in GitHub Desktop.
Save williamhqs/361ef70dd7584ff9837a30794f35780a to your computer and use it in GitHub Desktop.
Android Arrayadapter with text filtering for the use with a TextWatcher.
/**
* Arrayadapter (for Android) with text filtering for the use with a TextWatcher.
* Note: the objects in the List need a valid toString() method.
* @author Tobias Schürg
*
*/
public class FilteredArrayAdapter extends ArrayAdapter<ImageObject> {
private List<ImageObject> objects;
private Context context;
private Filter filter;
public CategoryAdapter(Context context, int resourceId, List<ImageObject> objects) {
super(context, resourceId, objects);
this.context = context;
this.objects = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public ImageObject getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return objects.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO: inflate your view HERE ...
return super.getView();
}
@Override
public Filter getFilter() {
if (filter == null)
filter = new AppFilter<ImageObject>(objects);
return filter;
}
/**
* Class for filtering in Arraylist listview. Objects need a valid
* 'toString()' method.
*
* @author Tobias Schürg inspired by Alxandr
* (http://stackoverflow.com/a/2726348/570168)
*
*/
private class AppFilter<T> extends Filter {
private ArrayList<T> sourceObjects;
public AppFilter(List<T> objects) {
sourceObjects = new ArrayList<T>();
synchronized (this) {
sourceObjects.addAll(objects);
}
}
@Override
protected FilterResults performFiltering(CharSequence chars) {
String filterSeq = chars.toString().toLowerCase();
FilterResults result = new FilterResults();
if (filterSeq != null && filterSeq.length() > 0) {
ArrayList<T> filter = new ArrayList<T>();
for (T object : sourceObjects) {
// the filtering itself:
if (object.toString().toLowerCase().contains(filterSeq))
filter.add(object);
}
result.count = filter.size();
result.values = filter;
} else {
// add all objects
synchronized (this) {
result.values = sourceObjects;
result.count = sourceObjects.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// NOTE: this function is *always* called from the UI thread.
ArrayList<T> filtered = (ArrayList<T>) results.values;
notifyDataSetChanged();
clear();
for (int i = 0, l = filtered.size(); i < l; i++)
add((ImageObject) filtered.get(i));
notifyDataSetInvalidated();
}
}
}
public class ListFilterActivity extends Activity{
private EditText filterText = null;
FilteredArrayAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
getListView().setTextFilterEnabled(true);
adapter = new FilteredArrayAdapter (this, 0, yourList);
setListAdapter(adapter);
}
/**
* Filter for filtering items in the list.
*/
private TextWatcher filterTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (adapter != null) {
adapter.getFilter().filter(s);
} else {
Log.d("filter", "no filter availible");
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment