I need to display the value at a specific position in my spinner but get a
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
error because my spinner uses a simple list set up as follows:
mSpinner = findViewById(R.id.spinner);
TextView msgTv = findViewById(R.id.errorMessage);
if (listTypes.size() > 0) {
/*
hide the error message
*/
msgTv.setVisibility(View.GONE);
/*
* the List has more than zero elements, so print them out in LogCat
*/
for(int i = 0; i < listTypes.size(); i++) {
Type type = listTypes.get(i);
int id = type.getId();
typeNameString = type.getType();
Log.d(TAG, "type " + typeNameString + " with an ID of: " + id);
}
//create spinner List<> of Type names only for use with the spinner
spinnerList = new ArrayList<>();
for (int i = 0; i < listTypes.size(); i++) {
typeNameString = listTypes.get(i).getType();
spinnerList.add(typeNameString);
Log.d(TAG, "Type " + i + " typeNameString is: " + typeNameString);
}
/*
* set the adapter to use 'this' context, the default Android spinner widget, typeCursor
* tyepCursor column as the source (from), display in the 'to' destination, no flags
*/
typeArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, spinnerList);
mSpinner.setAdapter(typeArrayAdapter);
} else{
/*
* no types exist in TABLE_TYPE, so add them
*/
msgTv.setText(R.string.no_types);
msgTv.setVisibility(View.VISIBLE);
}
My code to generate the spinner is as follows:
private void setProviderInfo(){
Log.d(TAG, "Entered setProviderInfo");
.
.
.
mSpinner.setAdapter(typeArrayAdapter);
mSpinner.setSelection(spinnerList.indexOf(type), true);
//TODO: try getView() to display the setSelection() value in spinner
ViewGroup view = (ViewGroup) findViewById(android.R.id.content);
typeArrayAdapter.getView(position, mSpinner, view);
.
.
.
}
My spinner's xml is:
<Spinner
android:id="@+id/spinner"
style="@style/Spinner"
android:prompt="@string/select_provider_type"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvProviderTitle" />
The style is:
<style name="Spinner">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">50dp</item>
<item name="android:layout_marginTop">8dp</item>
<item name="android:background">@android:color/holo_blue_bright</item>
<item name="android:layout_marginStart">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:layout_marginEnd">8dp</item>
<item name="android:clickable">true</item>
My spinner works that means that ArrayAdapter.getView() works on the ArrayList that is spinnerList using R.layout.support_simple_spinner_dropdown_item
, not a TextView. So, what do I need to do to get my direct call to getView()
to display the value at setSelection()
as happens when Android does it by default?
I have read the following (there are more), but didn't find anything useful as the initial, default call to ArrayAdapter.getView()
works without a TextView
widget in my xml:
ArrayAdapter requires the resource ID to be a TextView
“ArrayAdapter requires the resource ID to be a TextView” issue
ArrayAdapter requires the resource ID to be a TextView in DialogFragment
“ArrayAdapter requires the resource ID to be a TextView” xml problems
“ArrayAdapter requires the resource ID to be a TextView” error for AndroidX
Android ExpandableListAdapter: ArrayAdapter requires the resource ID to be a TextView