views:

96

answers:

3

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.

A: 

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.

BenTobin
The columns returned are _id and species. I am able to get values from each of the columns but the text is just not being displayed in the simple_spinner_dropdown_item. If I click on the spinner and select one of the blank rows then the dropdown list disappears and I am back to viewing my app and the value on the spinner is displaying the correct selected value. Its a very strange behavior.
A: 

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.

gthornejr
A: 

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 http://stackoverflow.com/questions/3073447/andorid-text-dropdown-selection-of-spinner-does-not-show/3075557#3075557