2012-12-13 117 views
0

任何人都可以確定我的代碼中存在什麼問題。AutoCompleteTextView退格問題

當按下退格鍵時,我的AutoCompleteTextView有奇怪的行爲。起初,當我在文本框中輸入時,它會顯示正確的值。比如我有串的名單即 「Pallanos」, 「帕拉納」, 「潘多爾」, 「Pasdi」。

當i型帕爾然後Pallanos帕拉納將顯示。但是當我按退格鍵建議的保存箱是不是刷新。我應該有四個結果,即上面的字符串。

我的繼承人適配器

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

回答

0

使用適配器,我發現我對我的問題的解決方案。但它有點慢。如果有人可以優化,然後隨意發佈您的答案。 :)

而是等同我的構造函數中objectthis.objects = collection)可變的,我把它裏面performFiltering。這個想法是每次刷新變量performFiltering變量的方法。

有些人在TextView中添加TextWatcher來刷新其適配器。

下面是我修改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; 
     } 

感謝