Android, spinner items not showing

2019-07-27 12:54发布

问题:

This is my code and I can not get the items add inside Spinner. I don't know what's going wrong, and unable to find any other way!

Java:

 spinner = (Spinner)getView().findViewById(R.id.spinner);

 String[] datos = getResources().getStringArray(R.array.items);

 ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, datos);
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spinner.setAdapter(adaptador);

XML:

<Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_margin="26dp"
        android:textColor="#FFF"/>

strings.xml:

<string-array name="items">
    <item >Item 1</item>
    <item >Item 2</item>
    <item >Item 3</item>
    <item >Item 4</item>
</string-array>

Thanks in advance for the help.

回答1:

your spinner text color is white: change it to other color.

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_margin="26dp"
    android:textColor="#000000"/>  //change this

also you can directly access the array resource in your layout. Like:

android:entries="@array/items" //add this in your spinner layout

EDIT

you can try using a custom layout for your spinner item:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"
    android:background="#000000"  //dark background
    android:text="Test"
    android:textColor="#ffffff"  //white text
    android:padding="5dp"
    />

use the layout using:

ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
        R.layout.spinner_item, datos);

here R.layout.spinner_item is the spinner custom layout



回答2:

In my case just added some padding in the XML file.

<Spinner android:id="@+id/spinnerMuestreo"
      android:layout_toEndOf="@+id/textViewNumMuestreo"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="8dp" <!--Try adding this line. -->
/>


回答3:

Try This XML Only

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_margin="26dp"
    android:textColor="@android:color/black"
    android:entries="@array/items"/>

Remove Below From Acticity

String[] datos = getResources().getStringArray(R.array.items);

 ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, datos);adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spinner.setAdapter(adaptador);


回答4:

<resources>
<string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
</string-array>

Spinner spinner = (Spinner) findViewById(R.id.spinner);
  // Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
 // Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner

 spinner.setAdapter(adapter);

on selected item

 public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...

public void onItemSelected(AdapterView<?> parent, View view,
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
 }

before that ..

spinner.setOnItemSelectedListener(this);