我正在尝试将 AutoCompleteTextView 设置为 ListView 标头,但如果我这样做,自动完成框永远不会出现。创建自动完成视图的代码直接来自谷歌文档中的Hello, AutoComplete教程。COUNTRIES 数组也来自那里。
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myList = (ListView) findViewById(R.id.ResultList);
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
TableLayout searchHeader = (TableLayout) layoutInflater.inflate(R.layout.search_header, null);
myList.addHeaderView(searchHeader, null, false);
final AutoCompleteTextView textView = (AutoCompleteTextView) myList.findViewById(R.id.edit);
ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(searchAdapter);
textView.setThreshold(1);
//Dummy data for listview.
String[] listContent = {
"test", "test", "test", "test",
"test", "test", "test", "test"
};
ArrayAdapter<String> adapter = new SearchResultAdapter(this, listContent);
myList.setAdapter(adapter);
}
作为测试,我添加了一个 TextChangedListener 来尝试强制显示对话框
textView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
textView.showDropDown();
}
});
对话框出现但几乎立即关闭。我想知道列表视图中的某种事件是否会导致这种情况?