0

I have written this code. It does not give error but gives wrong output. The date is displayed in times place and time in its place. I also have to set alarm. I am doing a to do app

Please help me to get my code correct and also set alarm..

My code:

public class Addmytask extends Activity{

static final int DATE_DIALOG_ID = 0;
private int mYear,mMonth,mDay;
EditText editText;

static final int TIME_DIALOG_ID=1;
 int hour,min;

 protected void onCreate(Bundle savedInstanceState) {
     // TODO Auto-generated method stub
     super.onCreate(savedInstanceState);
    setContentView(R.layout.addmytask);

     editText  = (EditText) findViewById(R.id.addd);
    editText  = (EditText) findViewById(R.id.description);

      Calendar c=Calendar.getInstance();
        mYear=c.get(Calendar.YEAR);
         mMonth=c.get(Calendar.MONTH);
         mDay=c.get(Calendar.DAY_OF_MONTH);
        hour=c.get(Calendar.HOUR);
        min=c.get(Calendar.MINUTE);

       dateformat();
       timeformat();
    }   

 private void dateformat() {
     // TODO Auto-generated method stub
    Calendar c=Calendar.getInstance();
     //String dateFormat = "dd/MM/yyyy";
         editText = (EditText) findViewById(R.id.text);
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
       editText.setText( sdf.format(c.getTime()));
       editText.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
           // TODO Auto-generated method stub
           showDialog(DATE_DIALOG_ID);
       }
   });
}

 private void timeformat() {
    // TODO Auto-generated method stub
     Calendar c=Calendar.getInstance();
     //string timeFormat = "HH:mm";
   editText =(EditText)findViewById(R.id.time);
    SimpleDateFormat stf = new SimpleDateFormat("HH:mm");
   editText.setText( stf.format(c.getTime()));  
   editText.setOnClickListener(new OnClickListener() {

        public void onClick(View v1) {
             // TODO Auto-generated method stub
             showDialog(TIME_DIALOG_ID);
          }
     });    
}

protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
    return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);

case TIME_DIALOG_ID:
    return new TimePickerDialog(this, timeSetListener, hour, min, false);

}

return null;
  }

private DatePickerDialog.OnDateSetListener mDateSetListener = new            DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {

    mYear = year;
    mMonth = monthOfYear;
    mDay = dayOfMonth;
    editText.setText(new     StringBuilder().append(mDay).append("/").append(mMonth+1).append("/").append(mYear));

}
 };

private TimePickerDialog.OnTimeSetListener timeSetListener=new  TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // TODO Auto-generated method stub

    hour=hourOfDay;
    min=minute;

editText.setText(new StringBuilder().append(hour).append(":").append(min));
}
};

//public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
//return true;
//}
}
Renjith
  • 5,783
  • 9
  • 31
  • 42
jan srkl
  • 55
  • 1
  • 9
  • why this? editText = (EditText) findViewById(R.id.addd); editText = (EditText) findViewById(R.id.description); – Abx May 28 '13 at 06:03
  • what happens when you put some hard coded values in there "e1" and "e2"? I don't see anything wrong, but I bet there's code you've trimmed out. – baash05 May 28 '13 at 06:09
  • In you onCreate method, initialize all your EditTexts with a different name eg: editTextTime = (EditText) findViewById(R.id.time); editTextDate = (EditText)findViewById(R.id.text) etc.. – nedaRM May 28 '13 at 06:13

1 Answers1

0

Dude do not try to reinvent the wheel use a Date Picker or Time Picker: Here are some links:

http://www.mkyong.com/android/android-date-picker-example/

http://www.mkyong.com/android/android-time-picker-example/

Cheers,

MSA
  • 2,502
  • 2
  • 22
  • 35