设置从Firebase获取的AutoCompleteTextView材料下拉列表的选定值

huangapple go评论20阅读模式
英文:

Setting selected value of AutoCompleteTextView Material drop down list from Firebase

问题

I am using AutoCompleteTextView material drop down list to add information to my Firebase database. This is working correctly and the information is being captured correctly.

我正在使用AutoCompleteTextView材质下拉列表将信息添加到我的Firebase数据库。这部分工作正常,信息被正确捕获。

I now want to allow my users to edit the information they have previously added to the database. I know how to update normal EditText views but when I use this same method with the AutoCompleteTextViews, the drop down only shows the value that is stored in Firebase and doesn't show the full drop down of options so the user can select a new value if they wish.

我现在希望允许用户编辑他们之前添加到数据库的信息。我知道如何更新普通的EditText视图,但当我在AutoCompleteTextView上使用相同的方法时,下拉列表只显示存储在Firebase中的值,不显示完整的下拉选项,以便用户可以选择新值(如果他们希望的话)。

The code I am using to show the currently selected values which are stored in the database for these drop downs is

我使用的代码来显示存储在数据库中的当前选定值如下:

String activityType = data.getStringExtra("Activity_Type");

我使用以下代码来填充下拉列表:

editActivityTypeLayout = findViewById(R.id.editActivityTypeLayout);
editActivityType = findViewById(R.id.editActivityType);

final String[] type = new String[]{
"Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
};

final ArrayAdapter editAdapterType = new ArrayAdapter<>(
EditActivity.this,
R.layout.dropdown_item,
type
);

editActivityType.setAdapter(editAdapterType);

However, this code doesn't populate the drop down list if I include the first line of code.

然而,如果我包含第一行代码,这段代码不会填充下拉列表。

Is there a different way to display the current value of the drop down and also display the other options?

是否有一种不同的方法来显示下拉列表的当前值并显示其他选项?

英文:

I am using AutoCompleteTextView material drop down list to add information to my Firebase database. This is working correctly and the information is being captured correctly.

I now want to allow my users to edit the information they have previously added to the database. I know how to update normal EditText views but when I use this same method with the AutoCompleteTextViews, the drop down only shows the value that is stored in Firebase and doesn't show the full drop down of options so the user can select a new value if they wish.

The code I am using to show the currently selected values which are stored in the database for these drop downs is

 String activityType = data.getStringExtra(&quot;Activity_Type&quot;);

I am then using this code to populate the drop down lists

editActivityTypeLayout = findViewById(R.id.editActivityTypeLayout);
    editActivityType = findViewById(R.id.editActivityType);

    final String[] type = new String[]{
            &quot;Formal Education Completed&quot;, &quot;Other Completed&quot;, &quot;Professional Activities&quot;, &quot;Self-Directed Learning&quot;, &quot;Work-Based Learning&quot;
    };

    final ArrayAdapter&lt;String&gt; editAdapterType = new ArrayAdapter&lt;&gt;(
            EditActivity.this,
            R.layout.dropdown_item,
            type
    );

    editActivityType.setAdapter(editAdapterType);

However, this code doesn't populate the drop down list if I include the first line of code.

Is there a different way to display the current value of the drop down and also display the other options?

答案1

得分: 11

要显示所选值,您应该使用:

AutoCompleteTextView editText = (AutoCompleteTextView) textInputLayout.getEditText();

editText.setAdapter(adapter);
editText.setText(value, false);

重要的是将过滤器设置为 false,以在下拉列表中显示整个列表,而不仅仅是单个值。

英文:

To display the selected value you should use:

   AutoCompleteTextView editText = (AutoCompleteTextView) textInputLayout.getEditText();

   editText.setAdapter(adapter);
   editText.setText(value,false);

It is important to set the filter false to display the entire list in dropdown and not only the single value.

huangapple
  • 本文由 发表于 2020年7月28日 00:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63119197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定