简体   繁体   中英

I don't understand Android AutoCompleteTextView Constructor of ArrayAdapter

I want to use AutoCompleteTextView in android and read the official developer.android documentation about it.

There is a code snippet which looks like:

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"
 };

I do not understand what the second parameter (android.R.layout.simple_dropdown_item_1line) in the constructor of ArrayAdapter means, where does this come from?

Is it a layout which is available from android or do I have to replace this layout with a layout created on my own and how to define this layout file in that case?

Concrete my code lokks like 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);

They are calling the constructor of ArrayAdapter with 3 arguments ( documentation ): ArrayAdapter(Context context, int resource, T[] objects)

The resource R.layout.simple_dropdown_item_1line is just one of the default android framework layouts for dropdowns. See here a list of other default layouts.

EDIT to answer your 2nd question :

You can either use the default android layout (the example you provided should work) or a custom one defined by you. If it's this last case then just create a xml layout for this layout:

layouts/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>

Then you can use the ArrayAdapter constructor ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) pointing to your custom layout and to the TextView you want to be populated with vocabs:

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

Also check that you're populating the vocabs array well.

这是Android系统中可用的布局,这是您使用android.R的原因。它用于显示数组适配器中的项目。它基本上是带有某些样式的textview

You can use the custom layout for AutoCompleteTextView like

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM