One spinner shows radio buttons, the next doesn

2019-07-31 15:38发布

I have two spinners, one above each other, like this:

<Spinner 
  android:layout_height="wrap_content"
  android:id="@+id/CitySpinner"
  android:layout_width="fill_parent"
  android:prompt="@string/city_prompt"
/>

<Spinner 
  android:layout_height="wrap_content"
  android:id="@+id/CountrySpinner"
  android:layout_width="fill_parent"
  android:prompt="@string/country_prompt"
/>

I set them like this

// set the data adapter for the city spinner
spnCity = (Spinner) findViewById(R.id.CitySpinner);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
    R.layout.simple_spinner_item,
    mDbHelper.getCities(),
    new String[] { KEY_CITY },
    new int[] {android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnCity.setAdapter(adapter);

// set the data adapter for the country spinner
spnCountry = (Spinner) findViewById(R.id.ProviderSpinner);
SimpleCursorAdapter scaCountries = new SimpleCursorAdapter(this,
    R.layout.simple_spinner_item,
    mDbHelper.getCountries(),
    new String[] { KEY_COUNTRY },
    new int[] {android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnCountry.setAdapter(scaCountries);

They both display the correct data, but the first has radio buttons and the second doesn't. Any ideas why?

(R.layout.simple_spinner_item is the same as android.R.layout.simple_spinner_item, except it has android:textColor="@color/black" added.)

1条回答
萌系小妹纸
2楼-- · 2019-07-31 16:12

In second spinner I think you have made a mistake Write

scaCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

So your code will be

// set the data adapter for the country spinner
spnCountry = (Spinner) findViewById(R.id.ProviderSpinner);
SimpleCursorAdapter scaCountries = new SimpleCursorAdapter(this,
    R.layout.simple_spinner_item,
    mDbHelper.getCountries(),
    new String[] { KEY_COUNTRY },
    new int[] {android.R.id.text1});
scaCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnCountry.setAdapter(scaCountries);
查看更多
登录 后发表回答