这里是:我正在打字" di",但是AutoCompleteTextView向我展示了无关的东西。
这是我如何开始这个:
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下拉错误的列表
答案 0 :(得分:0)
ArrayFilter类扩展了Filter:
更改: 如果(item.toLowerCase()。contains(prefixString)){
至: 如果(item.toLowerCase()。startsWith(prefixString)){
答案 1 :(得分:-1)
我想你可以从这个例子中找到一些提示:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_033);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView v = (AutoCompleteTextView)
findViewById(R.id.auto_complete_text_view);
v.setAdapter(adapter);
}
public void onButtonClick(View view) {
AutoCompleteTextView v = (AutoCompleteTextView)
findViewById(R.id.auto_complete_text_view);
Toast.makeText(this, v.getText(), Toast.LENGTH_SHORT).show();
}