im trying to add time and date from androids TimePickerDialog and DatePickerDialog into my SQLite database. However, when I try this, it doesnt work. No data appears in the database. I'm thinking that the dialogs are showing simultaneously as the data are being added to the database.
This is my addItem() function which is triggered with a button.
private void addItem(){
final Calendar calendar = Calendar.getInstance();
int HOUR = calendar.get(Calendar.HOUR);
int MINUTE = calendar.get(Calendar.MINUTE);
int YEAR = calendar.get(Calendar.YEAR);
int MONTH = calendar.get(Calendar.MONTH);
int DATE = calendar.get(Calendar.DATE);
//CREATES THE DATEPICKER DIALOG
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
dateString = year + " " + month + " " + dayOfMonth;
}
}, YEAR, MONTH, DATE);
//CREATES THE TIMEPICKER DIALOG
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeString = hourOfDay + ":" + minute;
}
}, HOUR, MINUTE, true);
datePickerDialog.show();
timePickerDialog.show();
ContentValues cv = new ContentValues();
cv.put(GroceryContract.GroceryEntry.COLUMN_DATE, dateString);
cv.put(GroceryContract.GroceryEntry.COLUMN_TIME, timeString);
mDatabase.insert(GroceryContract.GroceryEntry.TABLE_NAME, null, cv);
mAdapter.swapCursor(getAllItems());
}
It is working correctly when I move the ContentValues cv... code inside the onDateSet or onTimeSet methods. However, that seemed like bad practice.
Any ideas?