Android filter Listview freezes when softkeyboard is opend

in my application i have simple Arraylist as ListView from phone contacts lists and i’m try to filter that after type into edit text. after type into edittext my list view successfull filter by typed some characters. but list view dont refresh until softkeyboard is show and i must be hide keyboard or back to hide to refresh list view by filtered strings.

for example:

filter listview by “a”:

adapter.getFilter().filter("a");

My adapter implements from Filterable

public class AdapterContacts extends BaseAdapter implements Filterable {

    private LayoutInflater inflater;
    private Context context;
    private List<ContactLists> categoryArrayList;
    private final ArrayList<ContactLists> originalList = new ArrayList<ContactLists>();
    private NameFilter filter;

    public AdapterContacts(ArrayList<ContactLists> array) {
        categoryArrayList = array;
    }

    public AdapterContacts(Context context, List<ContactLists> array) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        categoryArrayList = array;
        originalList.addAll(array);
    }

    @Override
    public int getCount() {
        return categoryArrayList.size();
    }

    @Override
    public ContactLists getItem(int position) {
        return categoryArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
    }

    private static class UI extends HelperUI {
        ...
    }

    private class ViewHolder {
        private UI UI;

        public ViewHolder(View view) {
            UI = new UI(view);
        }

        public void fillItems(final AdapterContacts adapter, final ContactLists item, final int position) {
        ...
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new NameFilter();
        }
        return filter;
    }

    private class NameFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if (constraint != null && constraint.toString().length() > 0) {
                ArrayList<ContactLists> filteredItems = new ArrayList<ContactLists>();
                int l = originalList.size();
                for (int i = 0 ; i < l; i++) {
                    ContactLists nameList = originalList.get(i);
                    if (nameList.getContact_name().toString().contains(constraint)) {
                        filteredItems.add(nameList);
                    }
                }
                result.count = filteredItems.size();
                result.values = filteredItems;
            } else {
                synchronized (this) {
                    result.values = originalList;
                    result.count = originalList.size();
                }
            }
            return result;
        }


        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList<ContactLists> temp = (ArrayList<ContactLists>) results.values;
            categoryArrayList.clear();
            categoryArrayList.addAll(temp);
            notifyDataSetChanged();
            temp.clear();
            notifyDataSetInvalidated();
        }
    }
}


Source: android

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.