Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android Sqlite
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;
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();
					
					}
				});
			}
Posted 1-May-12 4:35am
Edited 1-May-12 5:48am
(no name)498.5K
v2

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 May 2012
Copyright © CodeProject, 1999-2014
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