How to set dropdown menu string array according to another dropdown menu (Android)?


This Content is from Stack Overflow. Question asked by Arda

I have 2 dropdown menus(1 -> autoCompleteTextProductType 2-> autoCompleteTextProductBrand)

I need to get the value of product type(it’s a String like: Mobile Phones, Computers etc.) from autoCompleteTextProductTypeand according to that value I need to change the resources of autoCompleteTextProductBrand For example if the chosen product type is = Mobile Phones then autoCompleteTextProductBrand resources would set to phone brands (Samsung Apple etc.)
I tried something like this:

    val productTypes = resources.getStringArray(R.array.productTypes)
    val productArrayAdapter = ArrayAdapter(requireActivity(),R.layout.dropdown_item,productTypes)
    //if autoCompleteTextProductBrand dropdown menu clicked get the right brand array. 
    //Then use the brand array for brand adapter

        val type = autoCompleteTextProductType.text.toString()
        val brands = returnProductBrandArray(type)
        val brandAdapter = ArrayAdapter(requireActivity(),R.layout.dropdown_item,brands)

    //This function is for getting the right string array. It needs a type. And returns 
    //string array directly.

    private fun returnProductBrandArray(type: String) : Array<String>{
        val brands = if(type == "Mobile Phones"){

       return brands

The issue in this solution is that when I click on dropdown menu first, no values are showing up. (I think that’s because I created the adapter inside the onclick listener)
But for the second time I click on it datas are showing up correctly. How can I solve this problem? I hope I summarized the issue well.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?