Search code examples
androidautocompletetextview

Android AutoCompleteTextView - Dropdown Not Showing


I'm putting in an autocompletetextview that I've used in another project, the code is copied over and works fine on the other project, yet it's not working for me now. My aim is to search in the database for a like description and return the results in the associated dropdown. I am getting results returned each time characters are entered, but the dropdown is not showing. The arraylist, adapter and autocompletetext view are all defined in a method on create and the text watcher is defined within the same method. Any ideas?

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="60dp"
                android:orientation="horizontal"
                android:background="@drawable/border"
                android:padding="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Description *:"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="5dp"/>

                <!--<EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtDescription"/>-->

                <AutoCompleteTextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/txtDescription"
                    android:textAppearance="@style/SmallText"
                    android:gravity="center_vertical"
                    android:inputType="textMultiLine"
                    android:layout_gravity="center_vertical"/>

            </LinearLayout>

        </LinearLayout>

ArrayList<String> descriptions = Shared.db.DB_Equipment.GetDescriptions("");
final ArrayAdapter<String> adapter = new ArrayAdapter<>(thisActivity,     android.R.layout.simple_dropdown_item_1line, descriptions);
txtDescription = (AutoCompleteTextView)rootView.findViewById(R.id.txtDescription);



txtDescription.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.length() > 1) {
                new AsyncTask<String, Void, ArrayList<String>>() {

                    @Override
                    protected ArrayList<String> doInBackground(String... params) {
                        ArrayList<String> descs = null;
                        try {
                            descs = Shared.db.DB_Equipment.GetDescriptions(charSequence.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return descs;
                    }

                    @Override
                    protected void onPostExecute(ArrayList<String> descriptions) {
                        adapter.clear();
                        if (descriptions != null)
                            for (String desc : descriptions) {
                                adapter.add(desc);
                            }
                        adapter.notifyDataSetChanged();
                    }
                }.execute(charSequence.toString());
            }
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

Solution

  • This line of code was missing in the onCreate():

    txtDescription.setAdapter(adapter);