By Amira Elsayed Ismail


2013-02-02 16:05:17 8 Comments

I have created a list view in android and I want to add edit text above the list and when the user enter text the list will be filtered according to user input

can anyone tell me please if there is a way to filter the list adapter in android ?

3 comments

@Puru Pawar 2013-02-02 16:16:23

Add an EditText on top of your listview in its .xml layout file. And in your activity/fragment..

lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name,    products);
lv.setAdapter(adapter);       
inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }

    @Override
    public void afterTextChanged(Editable arg0) {}
});

The basic here is to add an OnTextChangeListener to your edit text and inside its callback method apply filter to your listview's adapter.

EDIT

To get filter to your custom BaseAdapter you"ll need to implement Filterable interface.

class CustomAdapter extends BaseAdapter implements Filterable {

    public View getView(){
    ...
    }
    public Integer getCount()
    {
    ...
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                arrayListNames = (List<String>) results.values;
                notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                ArrayList<String> FilteredArrayNames = new ArrayList<String>();

                // perform your search here using the searchConstraint String.

                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mDatabaseOfNames.size(); i++) {
                    String dataNames = mDatabaseOfNames.get(i);
                    if (dataNames.toLowerCase().startsWith(constraint.toString()))  {
                        FilteredArrayNames.add(dataNames);
                    }
                }

                results.count = FilteredArrayNames.size();
                results.values = FilteredArrayNames;
                Log.e("VALUES", results.values.toString());

                return results;
            }
        };

        return filter;
    }
}

Inside performFiltering() you need to do actual comparison of the search query to values in your database. It will pass its result to publishResults() method.

@Amira Elsayed Ismail 2013-02-02 16:24:01

I have created a custom adapter that extends BaseAdapter and inside it I have defined a Vector of my object that will be shown in the list, when I try to use the above code I couldn't find getFilter method in my Adapter, so could you please tell me if I have to implement any interface ??

@Puru Pawar 2013-02-02 16:38:06

Filtering the data in case of BaseAdapter is a bit tricky. You will have to implement Filterable interface to your implementation of BaseAdapter. You will have then getFilter() method and inside it you have to implement two callback methods to work with; void publishResults() and FilterResults performFiltering(CharSequence constraint).

@Amira Elsayed Ismail 2013-02-02 16:39:43

can you support with a simple example please ?

@Puru Pawar 2013-02-02 16:49:16

Yes. Check EDIT section of my answer.

@Amira Elsayed Ismail 2013-02-03 09:09:13

Thank you very much, it is just what I need, but I have one more problem that when I use back space the list not returned back to it's original data, so can you please help in this problem ?

@Puru Pawar 2013-02-03 09:24:18

I suggest you to post another question on SO regarding this. Because it is not a proper way to keep asking different questions into same post. Well , as a heads up , just copy the whole arraylist into another temporary arraylist first and inside onPerformFiltering() method use this temporary list for searching. This will solve your problem. And please upvote and/or accept the answer if it helped you.

@Puru Pawar 2014-03-11 13:44:53

Its an ArrayList of the current contents of the ListView. Basically, the data set of your adapter.

@Iman Marashi 2016-07-11 21:09:02

Use import android.widget.Filter;

@jbrios777 2016-02-18 18:34:23

In case anyone are still interested in this subject, I find that the best approach for filtering lists is to create a generic Filter class and use it with some base reflection/generics techniques contained in the Java old school SDK package. Here's what I did:

public class GenericListFilter<T> extends Filter {

    /**
     * Copycat constructor
     * @param list  the original list to be used
     */
    public GenericListFilter (List<T> list, String reflectMethodName, ArrayAdapter<T> adapter) {
        super ();

        mInternalList = new ArrayList<>(list);
        mAdapterUsed  = adapter;

        try {
            ParameterizedType stringListType = (ParameterizedType)
                    getClass().getField("mInternalList").getGenericType();
            mCompairMethod =
                    stringListType.getActualTypeArguments()[0].getClass().getMethod(reflectMethodName);
        }
        catch (Exception ex) {
            Log.w("GenericListFilter", ex.getMessage(), ex);

            try {
                if (mInternalList.size() > 0) {
                    T type = mInternalList.get(0);
                    mCompairMethod = type.getClass().getMethod(reflectMethodName);
                }
            }
            catch (Exception e) {
                Log.e("GenericListFilter", e.getMessage(), e);
            }

        }
    }

    /**
     * Let's filter the data with the given constraint
     * @param constraint
     * @return
     */
    @Override protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        List<T> filteredContents = new ArrayList<>();

        if ( constraint.length() > 0 ) {
            try {
                for (T obj : mInternalList) {
                    String result = (String) mCompairMethod.invoke(obj);
                    if (result.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                        filteredContents.add(obj);
                    }
                }
            }
            catch (Exception ex) {
                Log.e("GenericListFilter", ex.getMessage(), ex);
            }
        }
        else {
            filteredContents.addAll(mInternalList);
        }

        results.values = filteredContents;
        results.count  = filteredContents.size();
        return results;
    }

    /**
     * Publish the filtering adapter list
     * @param constraint
     * @param results
     */
    @Override protected void publishResults(CharSequence constraint, FilterResults results) {
        mAdapterUsed.clear();
        mAdapterUsed.addAll((List<T>) results.values);

        if ( results.count == 0 ) {
            mAdapterUsed.notifyDataSetInvalidated();
        }
        else {
            mAdapterUsed.notifyDataSetChanged();
        }
    }

    // class properties
    private ArrayAdapter<T> mAdapterUsed;
    private List<T> mInternalList;
    private Method  mCompairMethod;
}

And afterwards, the only thing you need to do is to create the filter as a member class (possibly within the View's "onCreate") passing your adapter reference, your list, and the method to be called for filtering:

this.mFilter = new GenericFilter<MyObjectBean> (list, "getName", adapter);

The only thing missing now, is to override the "getFilter" method in the adapter class:

@Override public Filter getFilter () {
     return MyViewClass.this.mFilter;
}

All done! You should successfully filter your list - Of course, you should also implement your filter algorithm the best way that describes your need, the code bellow is just an example.. Hope it helped, take care.

@Cruces 2016-02-29 22:00:47

I don't know about android, but I remember being told to try to avoid reflection if possible in c# because it is quite resource intensive (I usually work on windows mobile applications so this could be a problem) , does this apply on android? or does reflection have the same effect as building an actual class without generics? I was thinking of creating a template for filterable and just adding the class and method used as parameters

@jbrios777 2016-08-03 12:56:08

Yes, you're correct. The same applies here, reflection gives a weight to the program processing, but in this case it's a very simple use of it, and because we are using it with a generic/template notation, it also helps the compiler. Good luck!

@Alexey 2018-10-19 16:45:11

N.B. You may have problems with obfuscation(dexguard/proguard) if you use reflection.

@M.Kouchi 2014-02-23 18:15:15

Implement your adapter Filterable:

public class vJournalAdapter extends ArrayAdapter<JournalModel> implements Filterable{
private ArrayList<JournalModel> items;
private Context mContext;
....

then create your Filter class:

private class JournalFilter extends Filter{

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults result = new FilterResults();
        List<JournalModel> allJournals = getAllJournals();
        if(constraint == null || constraint.length() == 0){

            result.values = allJournals;
            result.count = allJournals.size();
        }else{
            ArrayList<JournalModel> filteredList = new ArrayList<JournalModel>();
            for(JournalModel j: allJournals){
                if(j.source.title.contains(constraint))
                    filteredList.add(j);
            }
            result.values = filteredList;
            result.count = filteredList.size();
        }

        return result;
    }
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results.count == 0) {
            notifyDataSetInvalidated();
        } else {
            items = (ArrayList<JournalModel>) results.values;
            notifyDataSetChanged();
        }
    }

}

this way, your adapter is Filterable, you can pass filter item to adapter's filter and do the work. I hope this will be helpful.

Related Questions

Sponsored Content

97 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

48 Answered Questions

[SOLVED] Is there a unique Android device ID?

52 Answered Questions

38 Answered Questions

[SOLVED] How to lazy load of images in ListView in Android

48 Answered Questions

[SOLVED] You need to use a Theme.AppCompat theme (or descendant) with this activity

43 Answered Questions

34 Answered Questions

[SOLVED] Android Studio: Add jar as library?

18 Answered Questions

[SOLVED] Fling gesture detection on grid layout

Sponsored Content