2017-09-08 15 views
0

アンドロイドで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のコンストラクタの2番目のパラメータ(android.R.layout.simple_dropdown_item_1line)が何を意味するのか理解していないが、これは来ない場所から?

アンドロイドから利用可能なレイアウトですか、またはこのレイアウトを自分で作成したレイアウトで置き換える必要がありますか?その場合、このレイアウトファイルをどのように定義するのですか? XMLのような

コンクリート私のコードのlokks:

<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

ようAutoCompleteTextViewそれを交換する必要はありません、それは各行の私のデフォルトのレイアウトですあなたのオートコンプリートビューやカスタマイズされたレイアウトが必要な場合は、これを変更することがあります –

答えて

1

彼らは3つの引数(documentation)をArrayAdapterのコンストラクタを呼び出している:ArrayAdapter(Context context, int resource, T[] objects)

リソースR.layout.simple_dropdown_item_1lineはdの1つですドロップダウンのためのアンドロイドフレームワークレイアウトを作成します。他のデフォルトレイアウトのリストhereを参照してください。あなたはデフォルトアンドロイドレイアウト(あなたが提供される例が動作するはずです)、またはカスタムあなたによって定義されたものを使用することができますいずれか

:あなたの第二の質問に答える

EDIT。それはこの最後の場合だならば、ちょうどこのレイアウトの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> 

次に、あなたがあなたのカスタムレイアウトへとTextViewのにArrayAdapterコンストラクタArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)ポインティングを使用することができますあなたは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

.ITがアレイadapter.Itからアイテムを表示するために使用されますがandroid.Rを使用する理由は基本的にあなたがのためにカスタムレイアウトを使用することができますいくつかのスタイリング

0

とのTextViewでのAndroidのシステム内で利用可能である。このレイアウトは、

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> 
関連する問題