0

任何人都可以查明我的代码中的问题所在。

当我按下退格键时,我的 AutoCompleteTextView 有奇怪的行为。起初,当我在文本框上键入时,它会显示正确的值。例如,我有一个字符串列表,即“ Pallanos ”、“ Palana ”、“ Pandor ”、“ Pasdi ”。

当我输入Pal时,会显示PallanosPalana。但是当我按下退格键时,建议保管箱没有刷新。我应该有四个结果,即上面的字符串。

这是我的适配器

class EmployeeAdapter extends ArrayAdapter<EmployeeDetails>{

Context context; 
int layoutResourceId;
ArrayList<EmployeeDetails> data;
ArrayList<EmployeeDetails> objects;
//EmployeeDetails data[] = null;

public EmployeeAdapter(Context c, int paramlayoutResourceId, ArrayList<EmployeeDetails> collection) { //EmployeeDetails[] collection) {
    super(c, paramlayoutResourceId, collection);
    this.layoutResourceId = paramlayoutResourceId;
    this.context = c;
    this.data = collection;
    this.objects = collection;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if(row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, null);
    }

    EmployeeDetails emp = data.get(position);
    Departments dept = new Departments(context);
    AccountTypes accType = new AccountTypes(context);

    ((TextView) row.findViewById(R.id.tvMainHeader)).setText(emp.getFirstName() + ", " + emp.getLastName());
    ((TextView) row.findViewById(R.id.tvSubHeader)).setText(dept.GetDepartmentName(emp.getDepartmentID()).toString() + " - " + accType.GetAccountTypeName(emp.getAccountTypeID()).toString());
    ((ImageView) row.findViewById(R.id.imgHeader)).setImageResource(R.drawable.id_card);

    return row;
}

@Override
public Filter getFilter() {
    //return nameFilter;

    Filter nameFilter = new Filter() {

        public String convertResultToString(Object resultValue) {
            String str = ((EmployeeDetails)(resultValue)).getLastName();
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            ArrayList<EmployeeDetails> suggestions = new ArrayList<EmployeeDetails>();
            FilterResults filterresults = new FilterResults();

            if (constraint != null) {
                suggestions.clear();

                for (EmployeeDetails employee : objects) {
                    if (employee.getLastName().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                        suggestions.add(employee);
                    }
                }

                filterresults.values = suggestions;
                filterresults.count = suggestions.size();
            }

            return filterresults; 
        }

        @Override
        @SuppressWarnings("unchecked")
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList<EmployeeDetails> filteredList = (ArrayList<EmployeeDetails>) results.values;

            if (filteredList != null && filteredList.size() > 0) {
                clear();
                for (EmployeeDetails employee : filteredList) {
                    add(employee);
                }
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };

    return nameFilter;
} 
}

下面的代码显示了我如何在 AutoCompleteTextView 中使用适配器

    EmployeeAdapter adapter = new EmployeeAdapter(this, R.layout.list_row, empFunction.GetAllEmployees());

    AutoCompleteTextView empAuto = (AutoCompleteTextView)findViewById(R.id.auto_Employee);
    empAuto.setThreshold(1);
    empAuto.setAdapter(adapter);
4

1 回答 1

0

我找到了解决问题的方法。但它有点慢。如果有人可以优化,请随时发布您的答案。:)

我没有将object( this.objects = collection) 变量放在我的构造函数中,而是将它放在performFiltering. objects这个想法是在每次performFiltering方法触发时刷新变量。

有些人添加TextWatcher了 TextView 来刷新他们的适配器。

下面是我修改后的 perFormFiltering

            protected FilterResults performFiltering(CharSequence constraint) {
            EmployeeDetails empFunction = new EmployeeDetails(context);
            ArrayList<EmployeeDetails> suggestions = new ArrayList<EmployeeDetails>();
            FilterResults filterresults = new FilterResults();

            if (constraint != null) {
                suggestions.clear();
                objects = empFunction.GetAllEmployees();
                for (EmployeeDetails employee : objects) {
                    if (employee.getLastName().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                        suggestions.add(employee);
                    }
                }

                filterresults.values = suggestions;
                filterresults.count = suggestions.size();
            }

            return filterresults; 
        }

谢谢

于 2012-12-13T03:17:45.960 回答