In my Listview , I have text and image, and they are stored in an ArrayList .
I am using BaseAdapter to display a list.
I want to use filter in a list based on the text typed in EditText , but it doesn't work.
My code: adv - EditText
adv.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
And my BaseAdapter implements the Filterable so getFilter in that:
@Override public Filter getFilter() { // TODO Auto-generated method stub if (mFilter == null) { mFilter = new ArrayFilter(); } return mFilter; }
and my ArrayFilter :
private class ArrayFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { FilterResults results = new FilterResults(); if (mNames == null) { synchronized (mLock) { mNames = new ArrayList<String>(mObjects); } } if (prefix == null || prefix.length() == 0) { synchronized (mLock) { ArrayList<String> list = new ArrayList<String>(mNames); results.values = list; results.count = list.size(); } } else { String prefixString = prefix.toString().toLowerCase(); final ArrayList<String> values = mNames; final int count = values.size(); final ArrayList<String> newValues = new ArrayList<String>(count); for (int i = 0; i < count; i++) { final String value = values.get(i); final String valueText = value.toString().toLowerCase();
in that mNames is an ArrayList that I want to filter Listview .
and also included setTextFilterEnabled(true). but filtering does not work.
Is this the right approach? What's bad about it? if not, how to do it?
source share