简单 ArrayList 自动完成不显示

Simple ArrayList Autocomplete does not display

我正在尝试弹出一个自动完成列表,使用这个 link as a guide.

不过,当我开始为红色输入 "Re" 时,没有任何显示。

我做错了什么?

在我的activity

ArrayList<String> arrayColors = new ArrayList<String>();
arrayColors.add("Red");
arrayColors.add("Orange");
arrayColors.add("Yellow");
arrayColors.add("Green");
arrayColors.add("Blue");
arrayColors.add("Indigo");
arrayColors.add("Violet");

AutoCompleteTextView colorField = (AutoCompleteTextView) this.findViewById(R.id.editColor);
colorField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            handled = true;
        }
        return handled;
    }
});

ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayColors);
colorField.setAdapter(colorAdapter);
colorField.setThreshold(1);

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.mycompahy.myapp.DetailsActivity"
    tools:showIn="@layout/activity_details">

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:fillViewport="false"
        android:scrollbars="none">

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

    //OTHER CONTROLS

    <AutoCompleteTextView
        android:id="@+id/editColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:maxLength="30"
        android:maxLines="1" />


        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

我得到的错误信息:

W/Filter: An exception occured during performFiltering()! java.lang.NullPointerException: collection == null at java.util.ArrayList.(ArrayList.java:94) at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:456) at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.os.HandlerThread.run(HandlerThread.java:61)

我在尝试你的代码后发现了问题。您必须将一些宽度设置为 AutoCompleteTextView,然后您才能看到弹出窗口。例如将宽度设置为 match_parent.

<AutoCompleteTextView
    android:id="@+id/editColor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:maxLength="30"
    android:maxLines="1" />

这非常适合我。