Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android can't get EditText id after Date is picked in DatePicker

I have 3 EditText fields in my android form, each shows calendar datepicker after click. This works fine.

 sowin =(EditText) findViewById(R.id.sowin);
        sowout =(EditText) findViewById(R.id.sowout);
        ripe =(EditText) findViewById(R.id.ripe);

        DatePickerDialog.OnDateSetListener date =new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH,month);
                myCalendar.set(Calendar.DAY_OF_MONTH,day);

                EditText ide= findViewById(buttonClicked);
                updateLabel(ide);
            }
        };


        sowin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int buttonClicked = view.getId();
                new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        sowout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int buttonClicked = view.getId();
                new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        ripe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int buttonClicked = view.getId();
                new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

    private void updateLabel(EditText ide){
        String myFormat="dd.MM.yy";
        SimpleDateFormat dateFormat=new SimpleDateFormat(myFormat, Locale.US);
        ide.setText(dateFormat.format(myCalendar.getTime()));
    }

But I want to set the picked date to the correct field, from where the datepicker was inicialized.

That’s why I tried to add to each Click event int buttonClicked = sowin.getId(); to get the clicked field’s ID and then in onDateSet method

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EditText ide= findViewById(buttonClicked);
updateLabel(ide);

However I have a Nullpointerexception as the ID of the field is not known when trying to ide.setText in updateLabel method.

How do I obtain the correct field ID from where the datepicker was inicialized to correctly write it’s value?

>Solution :

You are creating new local variable on every onclickmethod which is not accessible in onDateSetListener, so make buttonClicked global and just initialize it from every onclick method.

 **int buttonClicked** //Global variable
    
    sowin =(EditText) findViewById(R.id.sowin);
            sowout =(EditText) findViewById(R.id.sowout);
            ripe =(EditText) findViewById(R.id.ripe);
    
            DatePickerDialog.OnDateSetListener date =new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int day) {
                    myCalendar.set(Calendar.YEAR, year);
                    myCalendar.set(Calendar.MONTH,month);
                    myCalendar.set(Calendar.DAY_OF_MONTH,day);
    
                    EditText ide= findViewById(buttonClicked);
                    updateLabel(ide);
                }
            };
    
    
            sowin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    **buttonClicked = view.getId();**    //just initialize here before opening datepicker 
                    new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
    
            sowout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    **buttonClicked = view.getId();**    //just initialize here before opening datepicker 
                    new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
    
            ripe.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    **buttonClicked = view.getId();**   //just initialize here before opening datepicker 
                    new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
    
        private void updateLabel(EditText ide){
            String myFormat="dd.MM.yy";
            SimpleDateFormat dateFormat=new SimpleDateFormat(myFormat, Locale.US);
            ide.setText(dateFormat.format(myCalendar.getTime()));
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading