我正在使用多个DatePickers,我想将字符串解析为日期。我想检查这两个日期是否正确,然后将它们插入到datebase中。我想使用标志,所以如果日期是正确的顺序标志将是真的,但它不起作用。
请帮忙!
这是代码:
public class Newreservation extends Activity {
public TextView startDateDisplay;
public TextView endDateDisplay;
public ImageButton startPickDate;
public ImageButton endPickDate;
public Calendar startDate;
public Calendar endDate;
public EditText email2, something;
public EditText mess;
public ImageButton send;
public static String flag = " ";
public static Date date11 = null;
public static Date date21 = null;
public static String ff, zz;
public static SimpleDateFormat form = new SimpleDateFormat("dd-MM-yyyy ");
static final int DATE_DIALOG_ID = 0;
public TextView activeDateDisplay;
public Calendar activeDate;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation);
email2=(EditText)findViewById(R.id.email2);
mess=(EditText)findViewById(R.id.mess);
something=(EditText)findViewById(R.id.something);
send = (ImageButton) findViewById(R.id.send);
Bundle bundle = getIntent().getExtras();
String email = bundle.getString("email");
String id_ad = bundle.getString("ad");
//Toast.makeText(getBaseContext(), " " + email + " " + id_ad, Toast.LENGTH_SHORT).show();
/* capture our View elements for the start date function */
startDateDisplay = (TextView) findViewById(R.id.begin);
startPickDate = (ImageButton) findViewById(R.id.beg);
/* get the current date */
startDate = Calendar.getInstance();
/* add a click listener to the button */
startPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(startDateDisplay, startDate);
}
});
/* capture our View elements for the end date function */
endDateDisplay = (TextView) findViewById(R.id.end2);
endPickDate = (ImageButton) findViewById(R.id.end);
/* get the current date */
endDate = Calendar.getInstance();
/* add a click listener to the button */
endPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(endDateDisplay, endDate);
}
});
/* display the current date (this method is below) */
updateDisplay(startDateDisplay, startDate);
updateDisplay(endDateDisplay, endDate);
ff = startDateDisplay.getText().toString();
zz = endDateDisplay.getText().toString();
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//HERE STARTS MY PROBLEM
try
{
date11 = (Date) form.parse(ff);
date21 = (Date) form.parse(ss);
if(date11.after(date21)){
//flag = "true";
Log.d("Nesto", "Date1 is after Date2");
}
else if(date11.before(date21)){
//flag = " ";
Log.d("WWWWW", "Date1 is before Date2");
}else if(date11.equals(date21)){
//flag = " ";
Log.d("WWWWW", "Date1 is equal to Date2");
}else{
//flag = "true";
Log.d("WWWWW", "How to get here?");
}
}
catch (Exception e)
{
e.printStackTrace();
}
//HERE ENDS MY PROBLEM
// TODO Auto-generated method stub
if(something.getText().toString().matches("") || email2.getText().toString().matches("") ||
mess.getText().toString().matches("") ||
startDateDisplay.getText().toString().equals(endDateDisplay.getText().toString() ||)
flag.matches(" "))
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Newreservation.this);
alertDialogBuilder.setTitle("Info");
alertDialogBuilder
.setMessage("You have not selected something or dates are wrong")
.setCancelable(false)
.setNegativeButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
else
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Newreservation.this);
alertDialogBuilder.setTitle("Info");
alertDialogBuilder
.setMessage("You have clicked: \n" + something.getText().toString() + "\n" + email2.getText().toString() + "\n"
+ mess.getText().toString() + "\n" + startDateDisplay.getText().toString() + "\n" + endDateDisplay.getText().toString() + "\n" )
.setCancelable(false)
.setNegativeButton("Next",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//HERE GOES CODE FOR INSERTING INTO DATABASE
Toast.makeText(getBaseContext(), "Inserted",
Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("Back",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
}
public void updateDisplay(TextView dateDisplay, Calendar date) {
dateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
.append(date.get(Calendar.MONTH) + 1).append("-")
.append(date.get(Calendar.YEAR)).append(" "));}
public void showDateDialog(TextView dateDisplay, Calendar date) {
activeDateDisplay = dateDisplay;
activeDate = date;
showDialog(DATE_DIALOG_ID);
}
public OnDateSetListener dateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
activeDate.set(Calendar.YEAR, year);
activeDate.set(Calendar.MONTH, monthOfYear);
activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(activeDateDisplay, activeDate);
unregisterDateDisplay();
}
};
public void unregisterDateDisplay() {
activeDateDisplay = null;
activeDate = null;
}
@Override
public Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
}
return null;
}
@Override
public void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
break;
}
}
}