-
-
Save tobiasschuerg/3554252 to your computer and use it in GitHub Desktop.
| /** | |
| * 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"); | |
| } | |
| } | |
| }; | |
| } |
thanks.
thanks
Worked Really Well. Thank you.
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
It works fine (y)
Fine! this work for me!
thanks!
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
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
Excelente <3 !!!!!!!!!!!!!
Excelente!!!