why does my code set values to the wrong view with listener
I have four Buttons that display the the date/time of calendars which are attributes of my activity. When a button is pressed, the according DatePickerDialog gets called which sets the values the users entered in the calendar object.
Though the identification of the calendars and dialogs seems correct, when I press "endDate" and click on ok in the dialog, startDate's text gets set.
Why does android behave like this and to fix this nonsense behaviour? (And yes, I cleaned and refreshed the project...)
Thank's in advance, you always surprise me guys :)
UPDATE OK, now I got it, I set the MONTH in the onTimeSetListener fr startDate instead of the MINUTE; fixed the code /UPDATE
Here's the listing: this is a part of the attribute definition (I left out the Views)
static final int START_DATE_DIALOG_ID = 0;
static final int START_TIME_DIALOG_ID = 1;
static final int END_DATE_DIALOG_ID = 2;
static final int END_TIME_DIALOG_ID = 3;
this is the relevant part of the onCreate()
startDate = (Button)findViewById(R.id.startDate);
startTime = (Button)findViewById(R.id.startTime);
endDate = (Button)findViewById(R.id.endDate);
endTime = (Button)findViewById(R.id.endTime);
startCal = Calendar.getInstance();
endCal = Calendar.getInstance();
[......]
startDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(START_DATE_DIALOG_ID);
}
});
startTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(START_TIME_DIALOG_ID);
}
});
endDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(END_DATE_DIALOG_ID);
}
});
endTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(END_TIME_DIALOG_ID);
}
});
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case START_DATE_DIALOG_ID:
return new DatePickerDialog(this,
startDateSetListener,
startCal.get(Calendar.YEAR),
startCal.get(Calendar.MONTH),
startCal.get(Calendar.DAY_OF_MONTH));
case START_TIME_DIALOG_ID:
return new TimePickerDialog(this,
startTimeSetListener,
startCal.get(Calendar.HOUR_OF_DAY),
startCa开发者_StackOverflowl.get(Calendar.MINUTE),
true); // is24hView
case END_DATE_DIALOG_ID:
return new DatePickerDialog(this,
endDateSetListener,
endCal.get(Calendar.YEAR),
endCal.get(Calendar.MONTH),
endCal.get(Calendar.DAY_OF_MONTH));
case END_TIME_DIALOG_ID:
return new TimePickerDialog(this,
endTimeSetListener,
endCal.get(Calendar.HOUR_OF_DAY),
endCal.get(Calendar.MINUTE),
true); // is24hView
}
return null;
}
private DatePickerDialog.OnDateSetListener startDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
startCal.set(Calendar.YEAR, year);
startCal.set(Calendar.MONTH, monthOfYear);
startCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
private DatePickerDialog.OnDateSetListener endDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
endCal.set(Calendar.YEAR, year);
endCal.set(Calendar.MONTH, monthOfYear);
endCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
private TimePickerDialog.OnTimeSetListener startTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
startCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
startCal.set(Calendar.MINUTE, minute);
updateDisplay(START_TIME_DIALOG_ID);
}
}
;
private TimePickerDialog.OnTimeSetListener endTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
endCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
endCal.set(Calendar.MINUTE, minute);
updateDisplay(END_TIME_DIALOG_ID);
}
}
;
// updates the date in the TextView
private void updateDisplay(int dialogId) {
switch(dialogId){
case START_DATE_DIALOG_ID:
startDate.setText(getDateFromCal(startCal));
break;
case START_TIME_DIALOG_ID:
startTime.setText(getTimeFromCal(startCal));
break;
case END_DATE_DIALOG_ID:
endDate.setText(getDateFromCal(endCal));
break;
case END_TIME_DIALOG_ID:
endTime.setText(getTimeFromCal(endCal));
break;
}
}
private String getDateFromCal(Calendar cal){
return new StringBuilder()
.append(cal.get(Calendar.DAY_OF_MONTH)) .append(".")
.append(cal.get(Calendar.MONTH) + 1) .append(".")
.append(cal.get(Calendar.YEAR)) .toString();
}
private String getTimeFromCal(Calendar cal){
StringBuilder sb = new StringBuilder();
int num = cal.get(Calendar.HOUR_OF_DAY);
if(num<10)
sb.append("0");
sb.append(num);
sb.append(":");
num = cal.get(Calendar.MINUTE);
if(num<10)
sb.append("0");
sb.append(num);
return sb.toString();
}
You're updating the wrong display (START_DATE_DIALOG_ID
) from your listener endDateSetListener
.
private DatePickerDialog.OnDateSetListener endDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
endCal.set(Calendar.YEAR, year);
endCal.set(Calendar.MONTH, monthOfYear);
endCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
Also - may I make a suggestion? It is very rare that a language or library is ever the problem; never assume that it is. Jeff Atwood (one of the guys that started StackOverflow) has an excellent article on the subject: First rule of programming: It's always your fault
精彩评论