我想設置一個AutoCompleteTextView作爲ListView標題,但如果我這樣做自動完成框永遠不會出現。用於創建自動完整視圖的代碼直接來自Google教程中的Hello, AutoComplete教程。 COUNTRIES數組也來自那裏。Android的AutoCompleteTextView作爲ListView標題
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();
}
});
出現對話框,但幾乎立即關閉。我想知道從列表視圖冒泡的某種事件是否造成這種情況?
你可以幫我這個http://stackoverflow.com/questions/29046302/how-to-implement-autocompletetextview-with- listview – 2015-03-16 06:11:48