Android Spinner not displaying list items
I think I am going crazy right now. I am trying to create a spinner populated by a datatable but for some reason the dropdown list items text is not being displayed. I have looked all over and开发者_运维百科 have seen other posts with people having this same problem. Can anyone help??
speciesList = (Spinner) findViewById(R.id.speciesList);
spinnerCursor = nsfdb.fetchAllSpecies();
startManagingCursor(spinnerCursor);
//String []cArrayList = new String[]{"dog", "cat", "horse", "other"};
String[] from = new String[]{"species"};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter locations = new SimpleCursorAdapter(this, R.layout.loc_row, spinnerCursor, from, to);
locations.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
speciesList.setAdapter(locations);
The spinner gets created just fine and is populated with 4 items but whenever I click on the spinner I see 4 items with no text and just radiobuttons. If I select any of them I am getting the correct selected item value but there is just no data displayed.
I experienced a similar problem when I tried copying and pasting code for setting up a ListView into a Spinner.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.array.location, android.R.layout.simple_list_item_1);
needed to be changed to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.array.location, android.R.layout.simple_spinner_item);
I would try check your layout setup in R.layout.loc_row to make sure it makes sense for the cursor data.
I had the same problem and was using: int[] to = new int[]{ R.id.text1};
I took
the above advice and changed it to: int[] to = new int[]{ android.R.id.text1};
Everything works fine now, Thanks!!
What columns are returned by fetchAllSpecies? I believe you'll need to have both a "_id" column and a "species" column present. If you only have one column, it may be using that for the id, but it has nothing to use for the text.
I don't know for sure if that's the problem you're experiencing. I haven't bound a spinner to a DB query myself.
User300339
I had the same issue. User Qberticus gave me a good pointer.
Basically when you specify the layout in the SimpleCursorAdapter locations
, you use the custom layout R.layout.loc_row
. The subsequent call setDropDownViewResource
will continue to use the same resource id bindings.
You can just simply use the following:
SimpleCursorAdapter locations = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, spinnerCursor, from, to);
locations.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
speciesList.setAdapter(locations);
This worked on my codes.
BTW, for the to array, you may use this instead.
int[] to = new int[]{ android.R.id.text1};
See details at Android - Text dropdown/selection of Spinner does not show
I got the same error first. I used
ArrayAdapter adapter = ArrayAdapter.createFromResource(getContext(),R.array.nitelik_array,android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
I deleted "simple_spinner_item"s and then rewrote it. It works. I don't know why..
精彩评论