简体   繁体   English

将初始值设置为 AutoCompleteTextView

[英]Set initial value to AutoCompleteTextView

Case: I have a screen (EditActivity) with a product creating/editing form.案例:我有一个带有产品创建/编辑表单的屏幕 (EditActivity)。 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.此下拉列表中的项目列表是通过 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() .我正在尝试通过setText()将产品类别初始值设置为该字段。 It works, and value appears in the dropdown.它有效,并且值出现在下拉列表中。 But other values from the ArrayAdapter disappear and I can't choose them.但是 ArrayAdapter 中的其他值消失了,我无法选择它们。 I also tried to set value using setSelection() , but I'm not sure this is correct way.我也尝试使用setSelection()设置值,但我不确定这是正确的方法。

Question: How do I insert the initial value into the dropdown so that I can see and select another values from the list?问题:如何将初始值插入下拉列表,以便我可以看到 select 列表中的其他值?

EditActivity layout part EditActivity布局部分

<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.此代码在 EditActivity 的 onCreate 方法中执行。

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)我如何尝试设置初始值 1(有效但未达到预期)

int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setText(arrayAdapter.getItem(itemPosition));

How I'm trying to set initial value 2 (looks like doesn't work)我如何尝试设置初始值 2(看起来不起作用)

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. setText()方法有第二个可选的 boolean 参数 - 过滤器。 This param is used to filter items from array adapter.此参数用于从数组适配器中过滤项目。

This worked for me in one of the kotlin projects I was working on.这在我正在从事的 kotlin 项目之一中对我有用。
I firstly set text of the AutoCompleteTextView.我首先设置 AutoCompleteTextView 的文本。 Then set an adapter to the AutoCompleteTextView.然后为 AutoCompleteTextView 设置一个适配器。
So the code should look like this;所以代码应该是这样的;

productCategoriesAutoCompleteView = findViewById(R.id.productCategoryAutoComplete);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this,R.array.product_categories,R.layout.list_item);

int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setText(arrayAdapter.getItem(itemPosition));
productCategoriesAutoCompleteView.setAdapter(arrayAdapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM