Skip to content

Instantly share code, notes, and snippets.

@tobiasschuerg
Created August 31, 2012 15:08
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tobiasschuerg/3554252 to your computer and use it in GitHub Desktop.
Save tobiasschuerg/3554252 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");
}
}
};
}
@guille150
Copy link

Excelente!!!

@arifmcc
Copy link

arifmcc commented Dec 5, 2015

thanks.

@levanchien
Copy link

thanks

@akshay-sarkar
Copy link

Worked Really Well. Thank you.

@demonsantosh
Copy link

I have shown server image in listview , for this how do i manage to filter it in from getFilter ....result is not being accurate in this case

@ErickDaniel
Copy link

It works fine (y)

@pedromen93
Copy link

Fine! this work for me!

thanks!

@hexa64
Copy link

hexa64 commented Mar 5, 2018

hello

I have "java.lang.String cannot be cast to com.search.test.Channel" where "Channel" is my Object

please can someone help

Thank You

@mgod
Copy link

mgod commented Jun 7, 2018

I don't believe my project (TokenAutoComplete) is using anything that would need permission to include. I read through this gist to figure out a reasonable approach to filtering an array adapter (thank you!), but wrote my own implementation. @tobiasschuerg you can see the file in question here if you would like to review it: https://github.com/splitwise/TokenAutoComplete/blob/master/library/src/main/java/com/tokenautocomplete/FilteredArrayAdapter.java

@javascript5
Copy link

Excelente <3 !!!!!!!!!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment