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!
Here is my dbadapter class
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!
Code:
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_a_trip);
mDbHelper = new MileageDbAdapter(this);
mDbHelper.open();
fillData();
}
//Fills the locations spinners with locations from the location table
private void fillData(){
Cursor locationsCursor = mDbHelper.fetchAllLocations();
startManagingCursor(locationsCursor);
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);
destinations.setAdapter(locations);
origins.setAdapter(locations);
//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);
}}
});
}
}
Code:
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
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(LOCATIONS_TABLE_CREATE);
db.execSQL(TRIPLOG_TABLE_CREATE);
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\')");
}
@Override
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(){
mDbHelper.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 + "\')");
}
}