Click here to Skip to main content
13,089,943 members (48,348 online)
Rate this:
Please Sign up or sign in to vote.

I am currently trying to retrieve and display information from a database using datePicker and timePicker in android. I've tried for weeks and have searched through the internet for solutions but havent found any and i'm currently stuck where the application crashes each time i click on the button to search through the database based on the time and date that i've specified. Here's my code. Any suggestions?

package sirtat.individualProject.PublicTransportationTimeScheduler;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.DatePicker;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.database.Cursor;
public class ScheduleActivity extends Activity {
	SQLiteDatabase db;
	TimePicker timePicker;
	DatePicker datePicker;
	int hour, minute;
	int yr, month, day;
	static String fDate="DepartureDate";
	static String fTime="DEPART_TIME";
	public static final String KEY_FLIGHTNO = "FLIGHT_NO";
	public static final String KEY_DLOCATION = "DEPART_LOCATION";
	public static final String KEY_ALOCATION = "ARRIVE_LOCATION";
	public static final String f_Arrival = "FlightArrival";
	/** Called when the activity is first created */
	public void onCreate(Bundle savedInstanceState) {
		db = openOrCreateDatabase("PublicTransport2" , SQLiteDatabase.CREATE_IF_NECESSARY, null);
		Calendar today = Calendar.getInstance();
	    yr= today.get(Calendar.YEAR);
		month = today.get(Calendar.MONTH);
		day = today.get(Calendar.DAY_OF_MONTH);
		hour = today.get(Calendar.HOUR);
		minute = today.get(Calendar.MINUTE);
		timePicker = (TimePicker) findViewById(;
		datePicker = (DatePicker) findViewById(;
		//--Button view--
				Button btnOp1 = (Button) findViewById(;
				btnOp1.setOnClickListener(new View.OnClickListener() {
					//inserting record in the database
					public void onClick(View v) {
						Cursor cur = fetchAllTodos();
						fDate = datePicker.getYear() +
								"/" + (datePicker.getMonth() +1) +
								"/" + datePicker.getDayOfMonth();
						fTime = timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute(); 
					startManagingCursor (cur);
			     	if (cur.moveToFirst()) {
			     		Toast.makeText(null, "Schedule Option 1 Selected:" + "\n" +
			     				"Flight No:" + cur.getString(0) + "\n" +
			     				"Depart Location:" + cur.getString(1) + "\n" +
			     				"Arrive Location:" + cur.getString(2), Toast.LENGTH_LONG).show();
						Toast.makeText(null, "Not found", Toast.LENGTH_LONG).show();
					Cursor fetchAllTodos(){
						Cursor cur=db.rawQuery("SELECT " +KEY_FLIGHTNO+ "," +KEY_DLOCATION+ "," +KEY_ALOCATION+  "FROM" +f_Arrival+ "WHERE DEPART_TIME = '" 
								+fTime+ "')" + " AND DepartureDate= '" +
										fDate+ "';", new String [] {fDate,fTime});
						return cur; 
					public void Insert(String FLIGHT_NO, String DEPART_LOCATION, String ARRIVE_LOCATION)
				       ContentValues data=createContentValues(FLIGHT_NO, DEPART_LOCATION, ARRIVE_LOCATION);
				       db.insert(f_Arrival, null, data);
				       Toast.makeText(null, "Record Inserted", Toast.LENGTH_SHORT).show();
					private ContentValues createContentValues(String FLIGHT_NO, String DEPART_LOCATION, String ARRIVE_LOCATION)     
				              ContentValues values = new ContentValues();
				              values.put(KEY_FLIGHTNO, FLIGHT_NO);
				  			  values.put(KEY_DLOCATION, DEPART_LOCATION);
				  	    	  values.put(KEY_ALOCATION, ARRIVE_LOCATION);
				              return values;
				Button btnOp2 = (Button) findViewById(;
				btnOp2.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
								"Date selected:" + datePicker.getYear() +
								"/" + (datePicker.getMonth() + 1) +
								"/" + datePicker.getDayOfMonth() + "\n" +
								"Time Selected:" + timePicker.getCurrentHour() +
								":" + timePicker.getCurrentMinute(),
Posted 1-May-12 3:35am
Updated 1-May-12 4:48am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 1 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100