简体   繁体   English

我不懂ArrayAdapter的Android AutoCompleteTextView构造函数

[英]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. 我想在android中使用AutoCompleteTextView并阅读有关它的官方developer.android文档。

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? 我不明白ArrayAdapter的构造函数中的第二个参数(android.R.layout.simple_dropdown_item_1line)是什么意思,它来自哪里?

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? 它是可从android获取的布局,还是我必须用自己创建的布局替换此布局,以及在这种情况下如何定义此布局文件?

Concrete my code lokks like xml: 具体我的代码象xml这样:

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

java: 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) 他们使用3个参数( 文档 )调用ArrayAdapter的构造函数: 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. 资源R.layout.simple_dropdown_item_1line只是下拉菜单的默认android框架布局之一。 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. 您可以使用默认的android布局(您提供的示例应该可以使用)或由您定义的自定义布局。 If it's this last case then just create a xml layout for this layout: 如果是最后一种情况,则只需为此布局创建一个xml布局:

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

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构造函数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);

Also check that you're populating the vocabs array well. 还要检查您是否很好地填充了vocabs数组。

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

You can use the custom layout for AutoCompleteTextView like 您可以使用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