Populating Android Spinner from SQLite Database

Populating Android Spinner from SQLite Database

In this tutorial we are going to learn how to populate android spinner from SQLIte database. If you are interested in reading how to use an array data to populate a spinner in android or how to display an image and text in android spinner, I will suggest you use the links above.

If you have a data that will not change in the entire life of your android application, the best way to store it might be as a string array in the string.xml file.

There are situations where by app users can be given the privilege to add more data that will be displayed in android spinner.

This is means that the data will change from time to time. In such a situation, it is better to use a database to store the data.

We will start by designing android SQLITE database that will house our data. There are different ways we can use to create our database. In this tutorial, I am going to use SQLITE Manager to create and manager the database file.

The database will be create using SQLite Expert Professional Manager which is a paid software but there are many free to use SQLite Manager you can get online and use.

I will go ahead and create a database called spinner. We have to create a table inside our database called content. The table will have two columns – id, and content. The SQLite code for the table is shown below.

Create Table [dictionary] (


[content] TEXT


Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidSpinnerDatabase

Package: com.inducesmile.androidspinnerdatabase

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

SQLITE Database

We will start from the backend and build the application to finish. The first thing we will do is to create a folder called databases inside assets folder. Since we created our SQLite database file using SQLite database manager, we will drag and drop the file inside the databases folder and zip.

We are preparing our database file this way since we are going to use a third party library called android sqlite asset helper. This library will help us to create and manage our database file. You can read more about this library here

The next thing to do is to import the library to our project. Since the library makes use of gradle, just open your build.gradle file and copy and paste the code below under the dependence section.

compile ‘com.readystatesoftware.sqliteasset:sqliteassethelper:+’

Now you can build your project and the library will be added automatically to your project. If you are using Eclipse for your android development, you can search on how to import library inside android project online.

We will go ahead with the SQLite database code now that we have finished setting up our database library.

We will create three different files – Database.java, DbObject.java and DbBackend.java.

The Database class will inherit from SQLiteAssetHelper which is a class from our imported library.

We will define our database name and version. The version number is use to control our application database update. Whenever there is a change in the database file, the version number will be updated. The code sample is shown below. Copy and paste it to your project file.

package inducesmile.com.androidspinnerdatabase;

import android.content.Context;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class Database extends SQLiteAssetHelper {

    private static final String DATABASE_NAMES = "spinner";

    private static final int DATABASE_VERSION = 1;

    public Database(Context context) {
        super(context, DATABASE_NAMES, null, DATABASE_VERSION);

We are going to establish a connection for our database and return the instance of our database in DbObject.java class. An instance of the Database is created and the getReadableDatabase of the class is used to get the connection object. The getter and close methods in the class is used to return and close the database connection. The code for this class is shown below.

package inducesmile.com.androidspinnerdatabase;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class DbObject {

    private static Database dbHelper;
    private SQLiteDatabase db;

    public DbObject(Context context) {
        dbHelper = new Database(context);
        this.db = dbHelper.getReadableDatabase();

    public SQLiteDatabase getDbConnection(){
        return this.db;

    public void closeDbConnection(){
        if(this.db != null){

Finally, to finish up with our backend, will create a queries in DbBackend.java file which will be use to retrieve data from our spinner table. The code is shown below.

package inducesmile.com.androidspinnerdatabase;

import android.content.Context;
import android.database.Cursor;

import java.util.ArrayList;

public class DbBackend extends DbObject {

    public DbBackend(Context context) {

    public String[] getAllSpinnerContent(){

        String query = "Select * from content";
        Cursor cursor = this.getDbConnection().rawQuery(query, null);
        ArrayList<String> spinnerContent = new ArrayList<String>();
                String word = cursor.getString(cursor.getColumnIndexOrThrow("content"));

        String[] allSpinner = new String[spinnerContent.size()];
        allSpinner = spinnerContent.toArray(allSpinner);

        return allSpinner;


Open your activity_main.xml file and drag and drop an android Spinner View in your layout file. You cam also use the code view in your IDE. Copy and paste the code below. It is very simple but right now the Spinner does not have a populated values in it.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_marginTop="20dp" />



Open MainActivity.java file, in this file we are going to get the instance of the Spinner view with findViewById() method of the activity class.

We will get the data stored in our database in a string array. The first thing to do is to create an object of our database query class (DbBackend.java) and then call its method getAllSpinnerContent() to return all the store data which will be use to populate our android spinner.

Next, we will create an instance ArrayAdapter that takes string input. We will pass our string array as pne of the parameters needed by the ArrayAdapter object.

We will call the setDropDownViewResource() of the Spinner View class and finally set the Spinner adapter using the setAdapter() method of the spinner class. An instance of our ArrayAdapter is passed as a parameter to the setAdapter method of the Spinner class.

The complete code for this class is shown below.

package inducesmile.com.androidspinnerdatabase;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

    private ArrayAdapter<String> listAdapter;

    protected void onCreate(Bundle savedInstanceState) {

        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        DbBackend dbBackend = new DbBackend(MainActivity.this);
        String[] spinnerLists = dbBackend.getAllSpinnerContent();

        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, spinnerLists);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            public void onNothingSelected(AdapterView<?> parent) {


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;

        return super.onOptionsItemSelected(item);

Save the file and run your project. If everything works for you, the project will appear like this in your device.

Populating Spinner from SQLite Database

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

Add a Comment