Search code examples
androidautocompletetextview

i have 2 AutoCompleteTextview how to populate both


I made 3 AutoCompleteTextview but when I populate it, only first one is populated and the other one is not working.

Note: I am using custom AutoCompleteTextview to resolve the threshold with 1 issue.

my layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".activities.ActivityProjectInfo2Preference">

    <!--toolbar-->
    <include layout="@layout/toolbar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="@dimen/dimens_10dp">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:layout_marginTop="@dimen/_16sdp"
                android:layout_marginStart="@dimen/_16sdp"
                android:text="Preferences"
                android:textColor="@color/colorBlack"
                android:textSize="@dimen/_18ssp"
                android:textStyle="bold" />


            <!--User of Room-->
            <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/dimens_16sp"
                android:paddingStart="@dimen/_14sdp"
                android:hint="User of Room">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/user_of_room"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </com.google.android.material.textfield.TextInputLayout>


            <!--style preference-->
            <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/_16sdp"
                android:paddingStart="@dimen/_14sdp"
                android:hint="Style Preferences">

                <ctdworld.com.eid.helper.InstantAutoComplete
                    android:id="@+id/activity_quiz_project_info_2_style_pref_spinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </com.google.android.material.textfield.TextInputLayout>
            <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/_16sdp"
                android:paddingStart="@dimen/_14sdp"
                android:hint="Color Preferences">

                <ctdworld.com.eid.helper.InstantAutoComplete
                    android:id="@+id/activity_quiz_project_info_2_color_pref_spinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </com.google.android.material.textfield.TextInputLayout>
            <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/_16sdp"
                android:paddingStart="@dimen/_14sdp"
                android:hint="Color Preferences">

                <ctdworld.com.eid.helper.InstantAutoComplete
                    android:id="@+id/activity_quiz_project_info_2_finish_pref_spinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

my java code :

String[] styleArray = getResources().getStringArray(R.array.entries_style_preference);
        ArrayAdapter styleAdapter = new ArrayAdapter(this,
                R.layout.dropdown_menu_popup_item,
                styleArray);
        styleAdapter.getFilter();
        mStylePrefSpinner.setAdapter(styleAdapter);

        String[] colorArray = getResources().getStringArray(R.array.entries_colors_preference);
        ArrayAdapter colorAdapter = new ArrayAdapter(this,
                R.layout.dropdown_menu_popup_item,
                colorArray);
        colorAdapter.getFilter();
        mStylePrefSpinner.setAdapter(colorAdapter);

        String[] finishArray = getResources().getStringArray(R.array.entries_finish_preference);
        ArrayAdapter finishAdapter = new ArrayAdapter(this,
                R.layout.dropdown_menu_popup_item,
                finishArray);
        finishAdapter.getFilter();
        mStylePrefSpinner.setAdapter(finishAdapter);

I tried it one by one then it started to work, but when i write code for all of the three, then only one of them is working.


Solution

  • You are assigning the adapter to the same binded property mStylePrefSpinner all the times:

    ...
    mStylePrefSpinner.setAdapter(styleAdapter);
    ...
    mStylePrefSpinner.setAdapter(colorAdapter);
    ...
    mStylePrefSpinner.setAdapter(finishAdapter);
    

    You should set it to each binded property. Assuming they are mStylePrefSpinner, mStyleColorSpinner & mStyleFinishSpinner:

    ...
    mStylePrefSpinner.setAdapter(styleAdapter);
    ...
    mStyleColorSpinner.setAdapter(colorAdapter);
    ...
    mStyleFinishSpinner.setAdapter(finishAdapter);