View filter list from edit text

I have an edit text as a search bar and a list view that filters the text that I typed, but unfortunately it does not filter the list view. I used a custom array adapter with a Friend object. The Friend object has a name, address, and phone number , but I only want to filter its name. In my work ...

searchBarTextView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    friendListAdapter.getFilter().filter(s);
}}

In the adapter ...

    @Override
    public Filter getFilter () {
        Log.d (TAG, "begin getFilter");
        if (newFilter == null) {
            newFilter = new Filter () {
                @Override
                protected void publishResults (CharSequence constraint, FilterResults results) {
                    // TODO Auto-generated method stub
                    Log.d (TAG, "publishResults");
                    notifyDataSetChanged ();
                }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                Log.d(TAG, "performFiltering");

                constraint = constraint.toString().toLowerCase();
                Log.d(TAG, "constraint : "+constraint);

                List<ChatObject> filteredFriendList = new LinkedList<ChatObject>();

                for(int i=0; i<friendList.size(); i++) {
                    Friend newFriend = friendList.get(i);
                    Log.d(TAG, "displayName : "+newFriend.getDisplayName().toLowerCase());
                    if(newFriend.getDisplayName().toLowerCase().contains(constraint)) {
                        Log.d(TAG, "equals : "+newFriend.getDisplayName());
                        filteredFriendList.add(newFriend);
                    }
                }

                FilterResults newFilterResults = new FilterResults();
                newFilterResults.count = filteredFriendList.size();
                newFilterResults.values = filteredFriendList;
                return newFilterResults;
            }
        };
    }
    Log.d(TAG, "end getFilter");
    return newFilter;
}

Can someone please help me how to show the filter of a filtered array correctly? I think notifyDataSetChanged is not being called. Thank.

+3
source share
1 answer

My problem is solved, it turned out that I have to override getCount () and getItem () .

+4
source

All Articles