Home > front end >  Android can't get EditText id after Date is picked in DatePicker
Android can't get EditText id after Date is picked in DatePicker

Time:04-13

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

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?

CodePudding user response:

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()));
        }
  • Related