0

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?

ajizi
  • 1

1 Answers1

0

When you call show on a dialog that dialog is just shown and there is no way of knowing what will be the selected value until the user selects it. That's why they have callbacks, its code that will be run asynchronous and the callback will be executed when there is a value. So your insert is in the wrong place as that values are not set yet.

I would create a method with the insert:

private void saveGroceryDate() {
    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());

}

and another with the time picker:

private void selectGroceryTime() {
    //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;
            saveGroceryDate();
        }
    }, HOUR, MINUTE, true);
}

So that after the date is selected (the DatePicker callback is executed) I would call the selectGroceryTime() method to select the time:

    //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;
            selectGroceryTime();
        }
    }, YEAR, MONTH, DATE);

And after the time is selected (in the TimePicker callback) I would save all the data in the database by calling saveGroceryDate() as in that moment all the data will be in the instance vars.

And keep in mind the user can cancel both dialogs, so check that your code behaves the way you want in that case. This could be important if that data is mandatory.

jeprubio
  • 17,312
  • 5
  • 45
  • 56