Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

New Forums: Nexus 6 | Nexus Player | Nexus 9
test: Reply
LinkBack Thread Tools
Old August 10th, 2011, 09:36 PM   #1 (permalink)
New Member
Thread Author (OP)
Join Date: Apr 2011
Posts: 3
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Issues writing spinner values to SQLite

Hey everyone, I have been working on an app for some time now and am stuck. This app is a mileage tracking app for my personal use at my work. I have 2 spinners set up with locations so that I can choose a beginning location and an ending location for my trip. The spinners populate their values from a SQLite database and this works fine.

The problem I am having is writing the spinner items that have been selected to a new table in the database. No matter what I do only the 2nd spinner value is recorded. Let me give an example;

Spinner Configuration1:

Spinner1 = Liberty
Spinner2 = Freedom

Database Output
Beginning Location Ending Location
Freedom Freedom

Spinner Configuration2:

Spinner1 = Westar
Spinner2 = Rainbow

Database Output
Beginning Location Ending Location
Rainbow Rainbow

So as you can see no matter what is chosen in the first spinner the end result is that the database is populated with the 2nd spinner's value in both the beginning and ending location fields.

Here is my code. Please help!

package com.shane.projects;

import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TimePicker;

public class MileageTrackerLogATripActivity extends MileageTrackerActivity {
    /** Called when the activity is first created. */

    public MileageDbAdapter mDbHelper;
    public void onCreate(Bundle savedInstanceState) {
        mDbHelper = new MileageDbAdapter(this);
    //Fills the locations spinners with locations from the location table
    private void fillData(){
        Cursor locationsCursor = mDbHelper.fetchAllLocations();
        String[] from = new String[]{MileageDbAdapter.LOCATIONS_COLUMN1};
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter locations = 
            new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,
                    locationsCursor, from, to);
        locations.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
        Spinner origins = (Spinner) findViewById(R.id.originSpinner);
        Spinner destinations = (Spinner) findViewById(R.id.destinationSpinner);
    //When the Log Trip button is pressed, settings are saved to DB
    final Button logTripBtn = (Button) findViewById(R.id.logTripBTN);
      logTripBtn.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              DatePicker pickdate = (DatePicker) findViewById(R.id.datePicker1);
              final int monthPicked = pickdate.getMonth();
              final int dayPicked = pickdate.getDayOfMonth();
              TimePicker pickTime = (TimePicker) findViewById(R.id.timePicker1);
              final int hourPicked = pickTime.getCurrentHour();
              final int minutePicked = pickTime.getCurrentMinute();
              final EditText startingMileage = (EditText) findViewById(R.id.beginningMileageET);
              final String startMiles = startingMileage.getText().toString();
              final EditText endingMileage = (EditText) findViewById(R.id.endingMileageET);
              final String endMiles = (String) endingMileage.getText().toString();
              EditText tripComments = (EditText) findViewById(R.id.commentsET);
              final String comment = (String) tripComments.getText().toString();
           //Gets and stores the selected spinner values
            Spinner originLocationSpinner = (Spinner) findViewById(R.id.originSpinner);
            Spinner destinationLocationSpinner = (Spinner) findViewById(R.id.destinationSpinner);
            final String originSpinnerSelected;
            final String destinationSpinnerSelected;
            Cursor c1 = (Cursor)(originLocationSpinner.getSelectedItem());
            Cursor c2 = (Cursor)(destinationLocationSpinner.getSelectedItem());
            if ((c1 != null) && (c2 != null)) {
                originSpinnerSelected = c1.getString(c1.getColumnIndex(mDbHelper.LOCATIONS_COLUMN1));
                destinationSpinnerSelected = c2.getString(c2.getColumnIndex(mDbHelper.LOCATIONS_COLUMN1));
              //Calls method in mileagDbAdapter.java that inserts the trip information into the database
              mDbHelper.logTripInfo(originSpinnerSelected, destinationSpinnerSelected, startMiles, endMiles, monthPicked, dayPicked, hourPicked, minutePicked, comment);

Here is my dbadapter class

package com.shane.projects;

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

public class MileageDbAdapter {
    //Declare database constants
    private static final String DATABASE_NAME = "MileageTrackerDB";
    private static final String DATABASE_TABLE1 = "locations";
    private static final String DATABASE_TABLE2 = "tripLog";
    private static final int DATABASE_VERSION = 1;
    //Declare locations table constants
    public static final String LOCATIONS_COLUMN1 = "location";
    public static final String LOCATIONS_ROWID = "_id";
    //Declare tripLog table constants
    public static final String TRIPLOG_COLUMN1 = "originLocation";
    public static final String TRIPLOG_COLUMN2 = "destinationLocation";
    public static final String TRIPLOG_COLUMN3 = "startingMileage";
    public static final String TRIPLOG_COLUMN4 = "endingMileage";
    public static final String TRIPLOG_COLUMN5 = "tripDate";
    public static final String TRIPLOG_COLUMN6 = "tripTime";
    public static final String TRIPLOG_COLUMN7 = "comments";
    public static final String TRIPLOG_ROWID = "_id";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    //Query to create Locations table stored in constant
    private static final String LOCATIONS_TABLE_CREATE = "create table " + DATABASE_TABLE1  + " ("
    + LOCATIONS_ROWID + " integer primary key autoincrement, " 
    + LOCATIONS_COLUMN1 + " text not null);"; 
    //Query to create TripLog table stored in constant
    private static final String TRIPLOG_TABLE_CREATE = "create table " + DATABASE_TABLE2  + " ("
    + TRIPLOG_ROWID + " integer primary key autoincrement, " 
    + TRIPLOG_COLUMN1 + " text, "
    + TRIPLOG_COLUMN2 + " text, "
    + TRIPLOG_COLUMN3 + " text, "
    + TRIPLOG_COLUMN4 + " text, " 
    + TRIPLOG_COLUMN5 + " integer, "
    + TRIPLOG_COLUMN6 + " integer, "
    + TRIPLOG_COLUMN7 + " text);"; 
    private final Context mCtx;
    public MileageDbAdapter(Context ctx) {
        this.mCtx = ctx;
    //Start Nested Class to create Database 
    private static class DatabaseHelper extends SQLiteOpenHelper{
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        //Create Tables and populate starting values
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("INSERT INTO locations (location) VALUES (\'Freedom\')");
            db.execSQL("INSERT INTO locations (location) VALUES (\'Liberty\')");
            db.execSQL("INSERT INTO locations (location) VALUES (\'Estrella\')");
            db.execSQL("INSERT INTO locations (location) VALUES (\'Westar\')");
            db.execSQL("INSERT INTO locations (location) VALUES (\'Rainbow\')");
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
                int newVersion) {

public MileageDbAdapter open() throws android.database.SQLException{
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
public void close(){
public Cursor fetchAllLocations() {
    return mDb.query(DATABASE_TABLE1, new String[] {LOCATIONS_COLUMN1, LOCATIONS_ROWID}, null, null, null, null, null);
public void logTripInfo(String originSpinnerSelected, String destinationSpinnerSelected, String startMiles, String endMiles, int month, int day, int hour, int minute, String com){
    mDb.execSQL("INSERT into tripLog (" + TRIPLOG_COLUMN1 + ", " + TRIPLOG_COLUMN2 + ", " + TRIPLOG_COLUMN3 + ", " 
            +  TRIPLOG_COLUMN4 + ", " + TRIPLOG_COLUMN5 + ", " + TRIPLOG_COLUMN6 + ", " + TRIPLOG_COLUMN7 + ") " +
                    "VALUES (\'" + originSpinnerSelected + "\', \'" + destinationSpinnerSelected + "\', \'" + startMiles + "\', \'" + 
                    endMiles + "\', \'" + month + "," + day + "\', \'" + hour + ":" + minute + "\', \'" + com + "\')");  


sway1101 is offline  
Reply With Quote
sponsored links
Old August 11th, 2011, 10:44 AM   #2 (permalink)
Join Date: Dec 2010
Location: Colorado (USA)
Posts: 153
Carrier: Not Provided

Thanks: 0
Thanked 22 Times in 22 Posts

Have you tried creating two cursors and two simplecursoradapters instead of using "locations" for both spinners?
mills2533 is offline  
Reply With Quote
spinner sqlite

Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

All times are GMT -5. The time now is 02:55 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.