Android – Populate a spinner with a SimpleCursor Adaptor

Let’s first create a Spinner view in the layout XML file

Spinner with cursor, <span>Android – Populate a spinner with a SimpleCursor Adaptor</span>, KandZ

Get the reference of the spinner:

//Class field
private Spinner spinner;

//inside onCreate
spinner = (Spinner) findViewById(R.id.spinner);

Now create a query with the data you want to populate the spinner

Spinner with cursor, <span>Android – Populate a spinner with a SimpleCursor Adaptor</span>, KandZ

Now create a new SimpleCursorAdapter:

Spinner with cursor, <span>Android – Populate a spinner with a SimpleCursor Adaptor</span>, KandZ
  • this – context
  • android.R.layout.simple_list_item_1 – a standard layout item
  • new String[] {GamesContract.GameEntry.COL_TITLE} – use the value of this columng
  • new int[]{android.R.id.text1} – the value should be displayed on this view

Now connect the cursor to the simpleCursorAdapter:

adapterGames.changeCursor(dbHelper.getGameTitles(db));

Set the adapter to the spinner:

spinner.setAdapter(adapterGames);

You can set/change the resource for the dropdown using setDropDownViewResource:

adapterGames.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner with cursor, <span>Android – Populate a spinner with a SimpleCursor Adaptor</span>, KandZ
Spinner with cursor, <span>Android – Populate a spinner with a SimpleCursor Adaptor</span>, KandZ

THE END!!!

98640cookie-checkAndroid – Populate a spinner with a SimpleCursor AdaptorAndroid – Populate a spinner with a SimpleCursor Adaptor

Related posts