带有过滤器的Android自动完成文本视图

时间:2018-07-24 04:54:39

标签: android delay freeze autocompletetextview

我正在使用自动完成的文本视图按任何字母进行过滤。 这个概念有效,但是当我输入每个字母时,应用程序将冻结或延迟一段时间。  同样在删除时,也会发生同样的情况。我究竟做错了什么?缺少什么吗?我的适配器类在下面添加。 预先感谢。

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();
            }
        }
    } 
};
}

3 个答案:

答案 0 :(得分:0)

我已经使用了AutoCompleteTextView及其正常工作。 我先将要呈现的内容放在String数组的AutoCompleteTextView中。 然后,在AutoCompleteTextView中设置适配器的内容。

 String[] arr=["a","b","c","d"]; // array contents
 ArrayAdapter<String> adapter=new ArrayAdapter<String> 
       (this,android.R.layout.simple_dropdown_item_1line,arr);
 AutoCompleteTextView autoCompleteTextView= 
       (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
 autoCompleteTextView.setThreshold(1);
 autoCompleteTextView.setAdapter(adapter);

答案 1 :(得分:0)

@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);
           //remove from here     notifyDataSetChanged();
            }
// put here  notifyDataSetChanged();
            }
        } 
    };

检查您的代码,您在循环中notifyDataSetChanged,我认为它会通知每个项目,这就是为什么冻结的原因,您能否检查一下以将其从循环中删除并在循环完成后通知。我认为它将起作用。

答案 2 :(得分:0)

问题已解决。 在MainActivity中设置适配器时,我已采取步骤

autotextview.setThreshold(1);

删除此行代码即可解决此问题。

感谢大家的帮助