19

I need to override a getFilter() method from the class ArrayAdapter and i found the source code from here in the github

//package name

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

public class CustomAdapter<T> extends ArrayAdapter<T> implements Filterable{

    private ArrayList<T> mOriginalValues;
    private List<T> mObjects;
    private CustomFilter mFilter;
    private final Object mLock = new Object();
    public CustomAdapter(Context context, int textViewResourceId, T[] objects) {
        super(context, textViewResourceId, objects);

        mObjects = Arrays.asList(objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        if (mFilter == null) {
            mFilter = new CustomFilter();
        }
        return mFilter;
    }


    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            Log.d("bajji", "its ---> " + prefix);
            if (mOriginalValues == null) {
                synchronized (mLock) {
                    mOriginalValues = new ArrayList<T>(mObjects);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<T> list;
                synchronized (mLock) {
                    list = new ArrayList<T>(mOriginalValues);
                }
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<T> values;
                synchronized (mLock) {
                    values = new ArrayList<T>(mOriginalValues);
                }

                final int count = values.size();
                final ArrayList<T> newValues = new ArrayList<T>();
                final ArrayList<T> approxValues = new ArrayList<T>();
                final ArrayList<T> secondApproxValues = new ArrayList<T>();


                for (int i = 0; i < count; i++) {
                    final T value = values.get(i);
                    final String valueText = value.toString().toLowerCase();
                    boolean flag = true;
                    // First match against the whole, non-splitted value
                    if (valueText.startsWith(prefixString)) {
                        newValues.add(value);
                        flag = false;
                    } else {
                        final String[] words = valueText.split(" ");
                        final int wordCount = words.length;

                        // Start at index 0, in case valueText starts with space(s)
                        for (int k = 0; k < wordCount; k++) {
                            if (words[k].startsWith(prefixString)) {
                                newValues.add(value);
                                flag = false;
                                break;
                            } 
                        }
                    }

                    if(flag) {
                        if(approxMatch(valueText, prefixString) <= 3) { //change the stuff and do a levi work
                            approxValues.add(value);
                        }
                        else {
                            final String[] words = valueText.split(" ");
                            final int wordCount = words.length;

                            // Start at index 0, in case valueText starts with space(s)
                            for (int k = 0; k < wordCount; k++) {
                                if(approxMatch(words[k], prefixString) <= 3) {
                                    //leve work
                                    secondApproxValues.add(value);
                                    break;
                                }
                            }
                        }
                    }
                }
                newValues.addAll(approxValues);
                newValues.addAll(secondApproxValues);
                results.values = newValues;
                results.count = newValues.size();
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mObjects = (List<T>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

    private int approxMatch (String s, String t) {
          // an approxmimate string matching algo
          return p;
    }
}

The problem is the getFilter method has a object of a private inner class ArrayFilter which has a method peformFiltering and i need to put a different code there so i have to overide the class. And i get an exception in the method.

In the derived class which extends ArrayAdapter i created a private inner class which is similar to ArrayFilter and called it MyFilter and i get the same exception again in the method performFiltering.

I found a solution to solve my problem. I copied all the code in ArrayAdapter class and created a new class called MyAdapter and i altered some code inside the inner class ArrayFilter and the app works the way i wanted it to. But i feel its not the best solution.

Android has various api levels so if the array adapter is changed in different api level then i have to add those changes in my codes to. So i feel the best way is to extend the class ArrayAdapter to create MyAdapter rather than just copying and pasting the code from the ArrayAdapter

How can i override the inner private class of a parent class..?

Edit: The exception i get.. Exception

Edit2: Now i added the full code in the question. and it works perfectly if i copy and edit the array adapter.. the problem is only when i extend..!! now the code and search is working perfectly. I checked it with Log.i.. but the drop down list for auto complete suggestion in UI is not working.. i only get for the first character i type the next character filtering takes place but UI update is not taking place.

Jeyanth Kumar
  • 1,589
  • 3
  • 23
  • 48
  • 1
    Can you tell what is that exception? – 5hssba Apr 30 '12 at 06:17
  • @raju i've added the exception i get in the question. – Jeyanth Kumar Apr 30 '12 at 06:31
  • why dont you simply override `getFilter()` in **ArrayAdapter**? – waqaslam Apr 30 '12 at 06:31
  • @Waqas see the source code. i have added the link. getFilter() has an object of a private inner class. and i need to override the methods inside the innerclass. – Jeyanth Kumar Apr 30 '12 at 06:36
  • 2
    you dont need to look for private inner class. simply create your own class which extends Filter and return it via getFilter() – waqaslam Apr 30 '12 at 07:23
  • i tried that.. it gives the same exception :( – Jeyanth Kumar Apr 30 '12 at 07:37
  • You can check my answer [here](http://stackoverflow.com/questions/8678163/list-filter-custom-adapter-dont-give-result/8678198#8678198) which I had done by using BaseAdapter. – Lalit Poptani May 04 '12 at 06:34
  • @Waqas i removed the exception by adding mObjects in constructor. With the help of Log.i i found out that perform filter method is working perfectly. Now the problem is when i type the first character i get the dropdown list box. But as i type the drop down list is not getting updated..! – Jeyanth Kumar May 07 '12 at 03:17
  • can you update your question with the code you are using at the moment? because i cant see any thing related to dropdown or autoCompleteEditText in your code – waqaslam May 07 '12 at 06:08
  • @Waqas I've posted the full CustomAdapter class in the question. And textwatcher in activity is same as the one liory answered... – Jeyanth Kumar May 07 '12 at 08:33

4 Answers4

11

After some help from stackoverflow community i removed the exception and later i found out that the suggestions that are returned doesn't really change because the mObjects was returned from the super class or base class. The problem was the there were two public methods getCount() and getItem(int position) which gets the count and fetches the list from mObjects from the base class. So i just have to add those two methods in my class..

public int getCount() {
    return mObjects.size();
}

public T getItem(int position) {
    return mObjects.get(position);
}

Now mObjects of the derived class will be returned. Which are the updated in the dropdown list in the UI.

I am not a java expert, but this solves my problem. If you have any suggestions to make the code better please add it in comments or feel free to edit the answer.!

Jeyanth Kumar
  • 1,589
  • 3
  • 23
  • 48
7

Update:

You should override the toString() method in your T class and return the filter criterion string.

For example,

class T {

    String firstName;
    String lastName;
    ...
    ...
    @override
    String toString() {
        return firstName + lastName;
    }
}

By default, the toString() implementation of the Object class returns getClass().getName() + '@' + Integer.toHexString(hashCode())

Original:

The array list reference should be passed to the ArrayAdapter constructor. I think you are not doing this and hence its not able to get a list for filtering.

In this sample code objects array reference is passed to the super constructor.

public CustomAdapter(Context context, int tvResId, ArrayList<String> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects;
}

If your filtering is based on the simple strings in the list items, then you can implement TextWatcher as suggested by @Iiorry. But if the filtering is bit complex than that, then you need to implement Filterable as well.

Ron
  • 24,175
  • 8
  • 56
  • 97
  • an alternative implementation of your suggestion removed the exception error. but the list box for suggestion is not updating as i type on... – Jeyanth Kumar May 04 '12 at 06:32
  • What type of item your arry list contains.. Please add the `performFiltering` code to your post.. And which suggestion helped u.. – Ron May 04 '12 at 06:35
  • You should override the `toString()` method for your `T` object. By default it returns the classname I guess.. – Ron May 04 '12 at 07:39
  • I don't understand why i should override toString().. the perform filter method is working perfectly the problem is the the result is not updated in the dropdown list in UI as i type.. – Jeyanth Kumar May 07 '12 at 03:13
  • U mean you get a filtered list in dropdown initially.. but its not updated after second character that you enter? Or you get an empty list initially? – Ron May 07 '12 at 04:25
  • I get a filtered list dropdown initially.. but its not updated after i type on the rest of the characters..!! I used Log.i to check `results.values` and it changes when i type.. but the update is not sent to UI.. – Jeyanth Kumar May 07 '12 at 05:07
  • Are you getting the whole list initially or the correct results only..? Also check what your `toString()` on T returns.. – Ron May 07 '12 at 05:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10949/discussion-between-jeyanth-kumar-and-userseven7s) – Jeyanth Kumar May 07 '12 at 05:39
  • Awarding the bounty as @userSeven7s spent a lot of time in helping me finding the solution...:) – Jeyanth Kumar May 10 '12 at 06:01
3

You don't need to implement your own filter... just override getFilter as @Waqas suggested.

Here's what I did and it works. Feel free to adjust it to your needs...

Hope it'll help.

The EditText (searchFilder):

searchFilter.addTextChangedListener(mOnSearchBoxTextChanged);
private TextWatcher mOnSearchBoxTextChanged = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            mAdapter.getFilter().filter(s.toString());
        }

    };

The Adapter getFilter():

    class MyAdapter extends BaseAdapter implements Filterable {

            //the rest of the adapter....

            @Override
            public Filter getFilter() {
                if (newFilter == null) {
                    newFilter = new Filter() {
                        @Override
                        protected void publishResults(CharSequence prefix,
                                FilterResults results) {

                            CLog.logD( "Friends list filtered");

                            mAdapter.notifyDataSetChanged();
                        }

                        @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    constraint = constraint.toString().toLowerCase();

                    filteredFriendList = new ArrayList<FriendsListItemView>();

                    if (constraint!= null && constraint.toString().length() > 0) {
                        for (int i = 0; i < friendsList.size(); i++) {
                            FriendsListItemView newFriend = friendsList.get(i);
                            String name = newFriend.userName.getText().toString();

                            if (name.toLowerCase().contains(constraint)) {
                                filteredFriendList.add(newFriend);
                            } 
                        }

                    } else {
                        if (filteredFriendList.size() == 0) {
                            filteredFriendList = friendsList;
                        }
                    }

                    FilterResults newFilterResults = new FilterResults();
                    newFilterResults.count = filteredFriendList.size();
                    newFilterResults.values = filteredFriendList;
                    return newFilterResults;
                }
            };
                }
                return newFilter;
            }
        }
Lior Iluz
  • 26,213
  • 16
  • 65
  • 114
1

Perhaps this may help you:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;

public class CustomAdapter<T> extends ArrayAdapter<T> {

    private final ArrayList<T> mOriginalValues;
    private List<T> mObjects;
    private CustomFilter mFilter;
    private final Object mLock = new Object();
    public CustomAdapter(Context context, int textViewResourceId, T[] objects) {
        super(context, textViewResourceId, objects);

        mObjects = Arrays.asList(objects);
        mOriginalValues = (ArrayList<T>) Arrays.asList(objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        if (mFilter == null) {
            mFilter = new CustomFilter();
        }
        return mFilter;
    }


    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            Log.d("bajji", "its ---> " + prefix);

            if (prefix == null || prefix.toString().trim().length() == 0) {
                ArrayList<T> list;
                synchronized (mLock) {
                    list = new ArrayList<T>(mOriginalValues);
                }
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<T> values;
                synchronized (mLock) {
                    values = new ArrayList<T>(mOriginalValues);
                }

                final int count = values.size();
                final ArrayList<T> newValues = new ArrayList<T>();
                final ArrayList<T> approxValues = new ArrayList<T>();
                final ArrayList<T> secondApproxValues = new ArrayList<T>();


                for (int i = 0; i < count; i++) {
                    final T value = values.get(i);
                    final String valueText = value.toString().toLowerCase();
                    boolean flag = true;
                    // First match against the whole, non-splitted value
                    if (valueText.startsWith(prefixString)) {
                        newValues.add(value);
                        flag = false;
                    } else {
                        final String[] words = valueText.split(" ");
                        final int wordCount = words.length;

                        // Start at index 0, in case valueText starts with space(s)
                        for (int k = 0; k < wordCount; k++) {
                            if (words[k].startsWith(prefixString)) {
                                newValues.add(value);
                                flag = false;
                                break;
                            } 
                        }
                    }

                    if(flag) {
                        if(approxMatch(valueText, prefixString) <= 3) { //change the stuff and do a levi work
                            approxValues.add(value);
                        }
                        else {
                            final String[] words = valueText.split(" ");
                            final int wordCount = words.length;

                            // Start at index 0, in case valueText starts with space(s)
                            for (int k = 0; k < wordCount; k++) {
                                if(approxMatch(words[k], prefixString) <= 3) {
                                    //leve work
                                    secondApproxValues.add(value);
                                    break;
                                }
                            }
                        }
                    }
                }
                newValues.addAll(approxValues);
                newValues.addAll(secondApproxValues);
                results.values = newValues;
                results.count = newValues.size();
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mObjects = (List<T>) results.values;
            notifyDataSetChanged();
            clear();
            for(T tmp : mObjects){
                add(tmp);
            }

            notifyDataSetChanged();
        }
    }

    private int approxMatch (String s, String t) {
          // an approxmimate string matching algo
          return p;
    }
}

Now in getView method of adapter, you need to refer to mObjects object to get recent available values for ListView

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • hmmm thats weird... in all other examples on net, i see people are using **clear();** without any problem. See [this](http://stackoverflow.com/a/2726348/966550) – waqaslam May 07 '12 at 09:25
  • instead of using Generics (T), can you change your code to a specific object type? – waqaslam May 07 '12 at 10:19
  • i found the solution and updated as the answer. Do check it and let me know if its a good way of doing it.. – Jeyanth Kumar May 08 '12 at 06:49