簡體   English   中英

我不懂ArrayAdapter的Android AutoCompleteTextView構造函數

[英]I don't understand Android AutoCompleteTextView Constructor of ArrayAdapter

我想在android中使用AutoCompleteTextView並閱讀有關它的官方developer.android文檔。

有一個代碼片段,如下所示:

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獲取的布局,還是我必須用自己創建的布局替換此布局,以及在這種情況下如何定義此布局文件?

具體我的代碼象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);

他們使用3個參數( 文檔 )調用ArrayAdapter的構造函數: ArrayAdapter(Context context, int resource, T[] objects)

資源R.layout.simple_dropdown_item_1line只是下拉菜單的默認android框架布局之一。 請參閱此處其他默認布局的列表。

編輯以回答您的第二個問題

您可以使用默認的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)指向您的自定義布局以及要使用vocab填充的TextView:

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

還要檢查您是否很好地填充了vocabs數組。

這是Android系統中可用的布局,這是您使用android.R的原因。它用於顯示數組適配器中的項目。它基本上是帶有某些樣式的textview

您可以使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM