2017-09-08 44 views
0

我想在android中使用AutoCompleteTextView並閱讀關於它的官方developer.android文檔。我不明白Android的AutoCompleteTextView ArrayAdapter的構造函數

有一個代碼片段,它看起來像:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
    AutoCompleteTextView textView = (AutoCompleteTextView) 
      findViewById(R.id.countries_list); 
    textView.setAdapter(adapter); 
} 

private static final String[] COUNTRIES = new String[] { 
    "Belgium", "France", "Italy", "Germany", "Spain" 
}; 

我不明白是什麼在ArrayAdapter的構造函數的第二個參數(android.R.layout.simple_dropdown_item_1line)意味着,哪裏該來從?

它是從android可用的佈局還是我必須用我自己創建的佈局替換此佈局以及如何在此情況下定義此佈局文件?

混凝土我的代碼lokks像 XML:

<AutoCompleteTextView 
     android:id="@+id/search" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

的java:

AutoCompleteTextView search =(AutoCompleteTextView) findViewById(R.id.search); 
String[] vocabs = new String[1001]; 
//fill the String array 
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line ,vocabs); 
search.setAdapter(adapter); 
+1

無需更換它,它是每一行的默認佈局我在您的自動完成視圖中,或者如果您需要自定義佈局,則可以更改此設置 –

回答

1

他們呼籲ArrayAdapter的構造與3個參數(documentation):ArrayAdapter(Context context, int resource, T[] objects)

資源R.layout.simple_dropdown_item_1line只是其中一個d下拉菜單的默認android框架佈局。請參閱here其他默認佈局的列表。

編輯回答你的第二個問題

您可以使用默認的Android的佈局(您提供的例子應該工作)或您定義的自訂。如果是後一種情況下則只是此佈局創建一個XML佈局:

佈局/ dropdown_custom_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:id="@+id/vocab_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:text="vocab"/> 
</LinearLayout> 

然後你可以使用一個ArrayAdapter構造ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)指向您的自定義佈局和TextView的你想填充vocabs:

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_custom_view, R.id.vocab_text ,vocabs); 
search.setAdapter(adapter); 

此外,檢查你是否填充好vocabs陣列。

+0

謝謝!但我希望在輸入前幾個字符時顯示下拉列表,但完全沒有任何反應......你知道這可能是什麼原因 – Jonas1902

+1

我剛剛在測試應用程序上試過這個演示,它對我來說效果很好。你是如何在xml中實現視圖的?我的代碼如下:'' –

+0

我在問題中添加了xml和java實現 – Jonas1902

0

這種佈局是Android系統這是你使用android.R。它是用來表示數組adapter.It項目的原因中可用基本上是一些造型一個TextView

0

您可以使用自定義佈局AutoCompleteTextView

ArrayAdapter<String> adapter = 
     new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.text_title, COUNTRIES); 
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list); 
textView.setAdapter(adapter); 

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#e4e4e4" 
    > 

    <TextView 
     android:id="@+id/text_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ellipsize="marquee" 
     tools:text="AA" 
     android:padding="15dp" 
     /> 
</LinearLayout> 
相關問題