Search code examples
androidautocompletetextview

Why is my AutoCompleteTextView not working?


This feels strange as normally the widgets work as they are supposed to. I have an AutoCompleteTextView that I want to populate with a list of city names. It seems simple but doesn't work as I intend to. Here is the resulting output:

enter image description here

Here is the layout in picture:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity1">

    <AutoCompleteTextView
        android:id="@+id/autocomptv_city_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/bg_edittext_rect_opd"
        android:hint="Select City"
        android:text=""
        android:inputType="text"
        android:maxLines="1"
        android:completionThreshold="0"
        android:padding="10dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textSize="15sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

And below is the java code for the same:

public class MainActivity1 extends AppCompatActivity {

    AutoCompleteTextView mAutCompleteTextViewSelfCity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);

        mAutCompleteTextViewSelfCity = ((AutoCompleteTextView) findViewById(R.id.autocomptv_city_list));

        setupCityDropdownwidget();
    }

    private void setupCityDropdownwidget() {
        Type listType = new TypeToken<List<CityName>>() {}.getType();
        List<CityName> citiesList = Singletons.getGsonInstance().fromJson(TestData.cityDataJson, listType);

        CityArrayAdapter adapter = new CityArrayAdapter(this, R.layout.item_spinner_city, citiesList);
        mAutCompleteTextViewSelfCity.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        mAutCompleteTextViewSelfCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CityName selectedCitySelf = ((CityName) parent.getItemAtPosition(position));
                mAutCompleteTextViewSelfCity.setText(selectedCitySelf.getCityName());
            }
        });
    }
}

Problem:

Well, I want the view to be such that as soon as the user taps on it, it shows a dropdown of cities and when the user starts typing their city for filtering, the view keeps showing narrowed down suggestions for the same.

Currently, the only time it is able to suggest is when I type in something and empty out the text view. If I change the completion threshold to 1, no suggestions are shown ever.

What's wrong with my code?

Here is the complete source for reference: https://wetransfer.com/downloads/ce4017f5f2488288ef7494dc029e033420191019092536/7afa9a3e64afb257293533bd634d6c3220191019092536/dc2341


Solution

  • So ultimately, it turned out to be about the basics - The data item that ArrayAdapter works with, should provide a meaningful toString() override. That is what had been missing from my implementation.

    From the docs:

    By default, the array adapter creates a view by calling Object#toString() on each data object in the collection you provide, and places the result in a TextView.

    I did end up wasting up some time but the experience and knowledge would surely come in handy some day.