Search code examples
androiddelayfreezeautocompletetextview

Android Autocomplete text view with filter


I am using an autocomplete textview to filter by any letter. The concept is working, but when I enter each letter, the application freezes or having a delay for some time. Also while deleting too, the same happens. What am I doing wrong? is there anything am missing? My adapter class is added below. Thanks in Advance.

public class ListAccNoAdapter extends ArrayAdapter<ListAccNo> {

Context context;
int resource, textViewResourceId;
List<ListAccNo> items, tempItems, suggestions;

public ListAccNoAdapter(Context context, int resource, int 
 textViewResourceId, List<ListAccNo> items) {
    super(context, resource, textViewResourceId, items);
    this.context = context;
    this.resource = resource;
    this.textViewResourceId = textViewResourceId;
    this.items = items;
    tempItems = new ArrayList<ListAccNo>(items); // this makes the 
     difference.
    suggestions = new ArrayList<ListAccNo>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_item, parent, false);
    }
    ListAccNo accnum = items.get(position);
    if (accnum != null) {
        TextView lblName = (TextView) view.findViewById(R.id.lbl_name);
        if (lblName != null)
            lblName.setText(accnum.getName());
    }
    return view;
}

@Override
public Filter getFilter() {
    return nameFilter;
}
/**
 * Custom Filter implementation for custom suggestions we provide.
 */
Filter nameFilter = new Filter() {
    @Override
    public CharSequence convertResultToString(Object resultValue) {
        String str = ((ListAccNo) resultValue).getName();
        return str;
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {

            List<ListAccNo> suggestions = new ArrayList<ListAccNo>();
            FilterResults filterResults = new FilterResults();

            for (ListAccNo accno : tempItems) {
                if (accno.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {                

                    suggestions.add(accno);
                }
            }

            filterResults.values = suggestions;
            filterResults.count = suggestions.size();


            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults 
    results) {
        List<ListAccNo> filterList = (ArrayList<ListAccNo>) 
   results.values;
        if (results != null && results.count > 0) {
            clear();
            for (ListAccNo accnum : filterList) {
                add(accnum);
                notifyDataSetChanged();
            }
        }
    } 
};
}

Solution

  • Problem Solved. While setting the adapter in MainActivity, i have given a step to

    autotextview.setThreshold(1);
    

    Removing this line of code solved the issue.

    Thanks Everyone for your help