Search code examples
javaandroidmaterial-uiandroid-textinputlayoutmaterial-components-android

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.

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("Activity_Type");

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[]{
            "Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
    };

    final ArrayAdapter<String> 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?


Solution

  • 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.