Search code examples
androidsqlitecursorspinnersimplecursoradapter

Unable to get selected item text from a spinner


In my application I have a spinner that displays a number of spending periods (weekly, bi-weekly, monthly, etc) and I filled it using a SimpleCursorAdapter because those periods are kept in the database. Here is how I implemented it:

Cursor spendingPeriodCursor = dataSource.getSpendingPeriods();
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, spendingPeriodCursor, fromColumns, toColumns);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpendingSpinner.setAdapter(mAdapter);

The spinner populates, and at the end of the activity I try to get the selected item like this:

// Get spending period description:
String spendingDesc = mSpendingSpinner.getSelectedItem().toString();

But it doesn't return the actual string value. It looks like a get a string representation of part of the cursor:

android.database.sqlite.SQLiteCursor@410dfae8

What do I have to do to get the actual text of the selected item?


Solution

  • Cursor cursor = (Cursor) mSpendingSpinner.getSelectedItem();    
    String text = cursor.getString(cursor.getColumnIndex("my_column_name"));