Click here to Skip to main content
15,891,864 members

How to retrieve and display information from a database using datePicker and timePicker in android

CookieMan90 asked:

Open original thread
hi,

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;
Java
import android.app.Activity;
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 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.schedule_layout);
		
		db = openOrCreateDatabase("PublicTransport2" , SQLiteDatabase.CREATE_IF_NECESSARY, null);
		
		db.setVersion(1);
		db.setLocale(Locale.getDefault());
		db.setLockingEnabled(true); 
		
		Calendar today = Calendar.getInstance();
		
		//--GET THE CURRENT DATE--
	    yr= today.get(Calendar.YEAR);
		month = today.get(Calendar.MONTH);
		day = today.get(Calendar.DAY_OF_MONTH);
		
		//--GET THE CURRENT TIME--
		hour = today.get(Calendar.HOUR);
		minute = today.get(Calendar.MINUTE);
		
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		
		datePicker = (DatePicker) findViewById(R.id.datePicker);
		
		
		
	
		
		//--Button view--
				Button btnOp1 = (Button) findViewById(R.id.btnOption1);
				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();
					}else{
						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(R.id.btnScheduleOption2);
				btnOp2.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Toast.makeText(getBaseContext(),
								"Date selected:" + datePicker.getYear() +
								"/" + (datePicker.getMonth() + 1) +
								"/" + datePicker.getDayOfMonth() + "\n" +
								"Time Selected:" + timePicker.getCurrentHour() +
								":" + timePicker.getCurrentMinute(),
								Toast.LENGTH_LONG).show();
					
					}
				});
			}
Tags: Mobile Apps (Android), Sqlite

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900