Donate

If any of the tips on this site have helped you out, consider buying me a cup of coffee. -dcPages
Android Tutorial: Populating Spinners - Populate Spinner using Database Query PDF Print E-mail
Written by dcPages   
Article Index
Android Tutorial: Populating Spinners
Populate Spinner Using String Resource
Populate Spinner using Database Query
Populate Spinner Manually
All Pages

Populating an Android Spinner from a database is pretty similar to doing it from an array resource like we did on the previous page. The XML file is the same and the Java code to reference the spinner and set the adapter is the same as well (for more details on that, view the previous page of this article on populating android spinners from a string array). The primary difference is that instead of using an Array Adapter to grab a string array and pass that to our spinner, instead, we're going to use a Simple Cursor Adapter to connect a database query to our spinner. To do that, you need to query your database, which is sorta outside the scope of this tutorial, so I won't go into how to create a database, build a table, add records or anything like that. However, I will mention that to use a Simple Cursor Adapter, you need to query your database in a way that returns a cursor, something like this:

Public Cursor fetchAllTitles(){
return mDb.query(
  table_name, new String[]{KEY_ROWID, KEY_TITLE}, null, null, null, null, null );
}
 

Note that you could also use something like "mDb.rawQuery(sql,null)" if you wanted to write your own SQL statement instead of using the query builder that comes with Android.

Once you have a method build to get that cursor, you need to call that method, and then build your Simple Cursor Adapter by passing it your cursor and a list of the fields from that cursor that you want to use. That should end up looking something like this:

private void fillSpinner(){
 
Cursor c = mDbHelper.fetchAllTitles();
startManagingCursor(c);
 
// create an array to specify which fields we want to display
String[] from = new String[]{field_name};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter adapter =
  new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
Spinner s = (Spinner) findViewById( R.id.unique_id );
s.setAdapter(adapter);
}
 

There are a few assumptions built in here. First, we're assuming that the "fetchAllTitles" method that we created earlier is part of a public class, and that you have referenced that class as a member of your current Java activity (using the name "mDbHelper"). Second, we are assuming that you have given your Spinner a unique id in the XML layout file - this code is actually assuming that the id you gave the spinner was "unique_id", but you can change that to whatever id you want to, so long as the one in your Java matches the one in your XML. Also, just like in the string array example, the view items that we referenced are a standard part of the Android Package, but you can use custom ones if you would like, which is a great way to spice up your spinners with colors, graphics, multiple data fields, etc.

That just about covers it for populating an Android Spinner from a database, in the next few days, I'll be adding a section on how to populate an Android Spinner manually on the next page.

 



 
 

Search

Polls

How Many Phone Numbers Should dcRedial Show?