0
這裏是:我輸入「di」,但AutoCompleteTextView顯示我無關的東西。AutoCompleteTextView顯示錯誤的結果
以下是我Init這:
autocompleteAdapter = new AutoCompleteDropdownAdapter(MainActivity.activity,
android.R.layout.simple_list_item_1, autoCompleteStrings);
etSearch.setAdapter(autocompleteAdapter);
這是我的適配器(我試過標準ArrayAdapter,但結果是一樣的)
class AutoCompleteDropdownAdapter extends ArrayAdapter<String> implements Filterable {
Activity main;
int rowLayout;
List<String> strings;
private ArrayList<String> originalValues;
private ArrayFilter filter;
public AutoCompleteDropdownAdapter(Activity main, int rowLayout, List<String> strings) {
super(main, rowLayout, strings);
this.main = main;
this.rowLayout = rowLayout;
this.strings = strings;
originalValues = new ArrayList<String>(strings);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new ArrayFilter();
}
return filter;
}
private class ArrayFilter extends Filter {
private Object lock;
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (originalValues == null) {
synchronized (lock) {
originalValues = new ArrayList<String>(strings);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
ArrayList<String> list = new ArrayList<String>(originalValues);
results.values = list;
results.count = list.size();
}
} else {
final String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values = originalValues;
int count = values.size();
ArrayList<String> newValues = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
String item = values.get(i);
if (item.toLowerCase().contains(prefixString)) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.values!=null){
ArrayList<String> tempStrings = (ArrayList<String>) results.values;
strings.clear();
strings.addAll(tempStrings);
}else{
strings = new ArrayList<String>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
現在,當我調試publishResults方法它顯示我正好需要2個結果,但不過,AutoCompleteTextView下降錯誤列表
做添加適配器的所有代碼,如果你跳過一些 – Rasel
沒有,這是?只是super.getView() – user2976267