In my app I have 2 buttons which opens a Time Dialog/Date Dialog and then I select the date and time and then press OK. Thefore the value gets shown in a TextView (for each one), so I need to get the VALUE from the TextViews and save it in the database.
database row = date
Code for Time shown in TextView:
tvHora = (TextView) findViewById(R.id.tvHora);
final Calendar d = Calendar.getInstance();
hour = d.get(Calendar.HOUR_OF_DAY);
minute = d.get(Calendar.MINUTE);
//set current time into textview
tvHora.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
Code for Date shown in TextView:
tvData = (TextView) findViewById(R.id.tvData);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
//SET CURRENT DATE INTO TEXTVIEW
tvData.setText(new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append("-"));
Code when I click the SAVE button:
ContentValues valor = new ContentValues();
valor.put("mensagenssalvas", resultado);
db.insert("mensagens", null, valor);
So how do I get the values from the textviews to save in the database?
EDIT
TimePicker Dialog:
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
// TODO Auto-generated method stub
hour = selectedHour;
minute = selectedMinute;
//set current time into textview
tvHora.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
}
};
DatePicker Dialog:
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int Year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year = Year;
month = monthOfYear;
day = dayOfMonth;
// set selected date into textview
tvData.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
I would store the value as an number in SQLite to make it compatible since SQLite doesn’t have built in date/time data types.
You will want to save the value in the DatePicke’s
OnDateSetListener.To give you an example on how to do this is exactly depends largely on how you create the date pickers. Assuming the values are set you can use the code below. Note that it assumes tvData and tvHora are in class properties (global variables to the class), this way you can reuse them once you set them (which should be done in onCreate)
Perhaps something like this would work:
In your xml layout for the file make a save button like this:
Or if you don’t want to put the onClick in xml, you can do the same thing in code like this: