2013-02-07 55 views
1

我想設置一個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(); 
     } 
    }); 

出現對話框,但幾乎立即關閉。我想知道從列表視圖冒泡的某種事件是否造成這種情況?

+0

你可以幫我這個http://stackoverflow.com/questions/29046302/how-to-implement-autocompletetextview-with- listview – 2015-03-16 06:11:48

回答

6

This question關於ListView中EditText視圖的焦點&對AutoCompleteTextView源碼的一些閱讀幫助我找到了答案。將ListView上的焦點順序設置爲afterDescendants可以正常顯示對話框。在autocompletetextview XML文件

<ListView 
    android:id="@+id/ResultList" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="50dip" 
    android:descendantFocusability="afterDescendants" 
    android:fadingEdge="none" > 
</ListView> 
+0

非常感謝你 – Gomino

+1

單詞不能表達歡樂哈哈! – micwallace

0

使用可能會幫助