-1

I'm trying to create a dynamic spinner (combobox), but I couldn't make it work, it is showing the first item in the spinner, but when I click it, the list with the other items is not showing and nothing is happening.

My activity xml:

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:orientation="horizontal">

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/global_languages"/>

        <Spinner
            android:id="@+id/localesSpinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:popupBackground="@color/facebookBlue"
            android:clickable="true"/>

    </LinearLayout>

I'm trying to set items this way:

private void loadLocalesSuccess(Collection<String> locales){
        Spinner localesSpinner = (Spinner) findViewById(R.id.localesSpinner);
        ArrayAdapter adapter = new ArrayAdapter<>(this,  android.R.layout.simple_spinner_item, new ArrayList(locales));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        localesSpinner.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

what could be the problem?

walter azevedo
  • 142
  • 2
  • 11
  • Please check the right way of implementation https://stackoverflow.com/questions/19079400/arrayadapter-in-android-to-create-simple-listview https://www.vogella.com/tutorials/AndroidListView/article.html – Bo Z Apr 29 '19 at 18:34
  • Are you sure something's not covering the `Spinner`, and consuming touch events? Do you see any visual feedback when you click it? – Mike M. Apr 29 '19 at 22:35
  • 1
    @MikeM., you are right, there was another TextView covering the spinner list. I don't know why yet, but the problem was it. Thanks to call my attention for this. – walter azevedo Apr 30 '19 at 03:33

1 Answers1

0

This code snippet should work, check if the data is proper.

    ArrayList<String> locales = new ArrayList<>();
    locales.add("English");
    locales.add("French");

    Spinner localesSpinner = findViewById(R.id.localesSpinner);
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, locales);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    localesSpinner.setAdapter(adapter);
    adapter.notifyDataSetChanged(); 
deepThought
  • 699
  • 4
  • 26