Recuperando valores de DatePicker Fecha actual recuperada

I have a DatePicker in my app, i'm using this code to retrieve its values:

    DatePicker d= (DatePicker) findViewById(R.id.datePicker1);
             int day = d.getDayOfMonth();
             int month = d.getMonth() + 1;
             int year = d.getYear();

             final String x=String.valueOf(day);
             final String y=String.valueOf(month);
             final String z=String.valueOf(year);

                 Button button1= (Button) (findViewById(R.id.button1));
                    button1.setOnClickListener(new View.OnClickListener() {
                         @Override
                        public void onClick(View v) {
Toast.makeText(SNCFTActivity.this, x+" " +y+ " "+z, Toast.LENGTH_LONG).show();

However, when tapping on button i always have the current date that is retrieved/displayed in Toast, not the values set in DatePicker. Can you please to help me out solving that problem? Thank you very much.

preguntado el 10 de marzo de 12 a las 16:03

2 Respuestas

use these inside your onClick función

    int day = d.getDayOfMonth();
     int month = d.getMonth() + 1;
     int year = d.getYear();

     final String x=String.valueOf(day);
     final String y=String.valueOf(month);
     final String z=String.valueOf(year);

respondido 10 mar '12, 16:03

OMG, what a mistake i did :(! I hate doing that mistakes ! Thank you very much Hesham, machkouur :). - androniennn

Try the code inside of onClick.

Algo como

  Button button1= (Button) (findViewById(R.id.button1));
                button1.setOnClickListener(new View.OnClickListener() {
                     @Override
                    public void onClick(View v) {

int day = d.getDayOfMonth();
         int month = d.getMonth() + 1;
         int year = d.getYear();

         final String x=String.valueOf(day);
         final String y=String.valueOf(month);
         final String z=String.valueOf(year);
Toast.makeText(SNCFTActivity.this, x+" " +y+ " "+z, Toast.LENGTH_LONG).show();
}

respondido 10 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.