不显示简单的ArrayList自动完成

时间:2016-03-25 14:42:06

标签: java android arraylist android-arrayadapter autocompletetextview

我正在尝试使用此link as a guide.

弹出一个自动填充列表

但是,当我开始为Red键入“Re”时,没有任何显示。

我做错了什么?

在我的活动中

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:performFiltering()期间发生异常!                                                                     java.lang.NullPointerException:collection == null                                                                         在java.util.ArrayList。(ArrayList.java:94)                                                                         在   android.widget.ArrayAdapter $ ArrayFilter.performFiltering(ArrayAdapter.java:456)                                                                         在android.widget.Filter $ RequestHandler.handleMessage(Filter.java:234)                                                                         在android.os.Handler.dispatchMessage(Handler.java:102)                                                                         在android.os.Looper.loop(Looper.java:145)                                                                         在android.os.HandlerThread.run(HandlerThread.java:61)

1 个答案:

答案 0 :(得分:0)

我在尝试代码后发现了问题。您必须将某个宽度设置为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" />

这对我来说很有效。