Reputation: 283
I have a datepicker and a time picker in a custom dialog. If I change the date on the datepicker it gets updated on the Textview. But when I select the datepicker again, it gives today's date. I want the previous selected date to show on the datepicker. This is my code. Pls help!!
from.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
picker = new Dialog(getActivity());
picker.setContentView(R.layout.datetime);
picker.setTitle("Select Date and Time");
datep = (DatePicker) picker.findViewById(R.id.datePicker);
timep = (TimePicker) picker.findViewById(R.id.timePicker1);
set = (Button) picker.findViewById(R.id.btnSet);
set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
month = datep.getMonth() + 1;
day = datep.getDayOfMonth();
year = datep.getYear();
hour = timep.getCurrentHour();
minute = timep.getCurrentMinute();
from.setText(year + "-" + month + "-" + day + " "
+ hour + ":" + minute);
picker.dismiss();
}
});
picker.show();
}
});
Upvotes: 2
Views: 3367
Reputation: 283
Found the answer. Just set it outside by scope of the button which shows the button...
picker = new Dialog(getActivity());
picker.setContentView(R.layout.datetime);
picker.setTitle("Select Date and Time");
datep = (DatePicker) picker.findViewById(R.id.datePicker);
timep = (TimePicker) picker.findViewById(R.id.timePicker1);
from.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
set = (Button) picker.findViewById(R.id.btnSet);
set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
//datep.updateDate(year, month, day);
month = datep.getMonth() + 1;
day = datep.getDayOfMonth();
year = datep.getYear();
hour = timep.getCurrentHour();
minute = timep.getCurrentMinute();
from.setText(year + "-" + month + "-" + day + " "
+ hour + ":" + minute);
picker.dismiss();
}
});
picker.show();
}
});
Upvotes: 2
Reputation: 541
You have 2 options:
1) save thre previous selected day, month and year in a variable - and set them into the DatePickerDialog
2) get the saved date from your text - and set them into the DatePickerDialog
Upvotes: 0
Reputation: 609
You would need to add a OnDateSetListener on the DatePicker Dialog, something like this :
/**
* Initialize DatePickerDialog
*/
private void initDatePicker() {
myCalendar = Calendar.getInstance();
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
dateDialog = new DatePickerDialog(getActivity(), date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
dateDialog.getDatePicker().setMaxDate(new Date().getTime());
}
Once you initialise this method, it will automatically show the date on the datepicker once it has been set. (:
Upvotes: 0