我从九月开始就在一个Android项目上,我将处理AutocompleteTextView。 我让我的personnal ArrayAdapter填充下拉列表建议列表,它可以工作,但行为奇怪。
当我在下拉列表中选择一个项目时,它会通过选择替换textView中的文本(没关系),但在此之后,请重新显示下拉列表。 我希望它隐藏,直到用户在键盘上键入其他内容。
以下是我的代码示例:
private List<String> autocompleteAddress;
addressBox = (AutoCompleteTextView)findViewById(R.id.form_address_box);
autocompleteAdapterAddress = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, autocompleteAddress){
@Override
public Filter getFilter(){
Filter custom_filter = new Filter(){
@Override
protected FilterResults performFiltering(CharSequence constraint){
FilterResults filterResults = new FilterResults();
filterResults.values = autocompleteAddress;
filterResults.count = autocompleteAddress.size();
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint, FilterResults results){
notifyDataSetChanged();
}
};
return custom_filter;
}
};
addressBox.setAdapter(autocompleteAdapterAddress);
addressBox.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
//ac is an asynctask populating the suggestion list (autocompleteAddress)
GetGoogleAutocompletion ac = new GetGoogleAutocompletion();
ac.execute(addressBox.getText().toString());
}
@Override public void afterTextChanged(Editable s){}
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after){}
});
addressBox.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// here i just store the autocompleted text in a String
}
});
我认为setText()方法将在onItemClick()中调用,我可以使用dismissDropDown(),但是当我重写onItemClick()时,我很确定它不存在。
所以我真的不知道调用setText()的位置,也不知道如何在选择后避免显示dropDown菜单。
提前致谢!