Case: I have a screen (EditActivity) with a product creating/editing form. This screen has a layout in which there is a drop-down list product category. The list of items in this drop-down is set via ArrayAdapter.createFromResource. I don't have a problem when I use this screen as a creating form. The problem appears when I use this screen as a editing form. I'm trying to set the product category initial value to the field via setText()
. It works, and value appears in the dropdown. But other values from the ArrayAdapter disappear and I can't choose them. I also tried to set value using setSelection()
, but I'm not sure this is correct way.
Question: How do I insert the initial value into the dropdown so that I can see and select another values from the list?
EditActivity layout part
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/productCategory"
android:hint="@string/ed_product_category"
...
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
<AutoCompleteTextView
android:id="@+id/productCategoryAutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
Setting list items to autocompletefield. This code is executed inside onCreate method of EditActivity.
productCategoriesAutoCompleteView = findViewById(R.id.productCategoryAutoComplete);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(
this,
R.array.product_categories,
R.layout.list_item
);
productCategoriesAutoCompleteView.setAdapter(arrayAdapter);
How I'm trying to set initial value 1 (work but not as expected)
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setText(arrayAdapter.getItem(itemPosition));
How I'm trying to set initial value 2 (looks like doesn't work)
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setSelection(itemPosition);
Field in creation form
Field in updating form
What I want in updating form
setText()
method has second optional boolean parameter - filter. This param is used to filter items from array adapter.