Home > Enterprise >  Android: CalendarView and Button not able to share data
Android: CalendarView and Button not able to share data

Time:12-17

I'm creating an application that lets user to list down school assignment and the deadline.

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_layout);
        mCalendarView = (CalendarView) findViewById(R.id.calendarView);
        btnConfirmCal = (Button) findViewById(R.id.confirmCalendar);
        et_assignName = (EditText) findViewById(R.id.assignName);
        et_assignDesc = (EditText) findViewById(R.id.assignDesc);

        mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {

                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                String selectedDate = sdf.format(new Date(mCalendarView.getDate()));
            }
        });
        btnConfirmCal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    ScheduleList scheduleList = new ScheduleList(et_assignName.getText().toString(),
                            et_assignDesc.getText().toString(), selectedDate);
                    Toast.makeText(CalendarActivity.this, scheduleList.toString(), Toast.LENGTH_SHORT).show();
                }
                catch (Exception e) {
                    Toast.makeText(CalendarActivity.this, "Error creating schedule", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

In the OnClickListerner, the "selectedDate" is highlighted as an error. Is there a way to solve the issue?

CodePudding user response:

The selectedDate is defined in another scope so it is not visible for OnClickListerner. You can move it to the onCreate method scope.

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendar_layout);
    mCalendarView = (CalendarView) findViewById(R.id.calendarView);
    btnConfirmCal = (Button) findViewById(R.id.confirmCalendar);
    et_assignName = (EditText) findViewById(R.id.assignName);
    et_assignDesc = (EditText) findViewById(R.id.assignDesc);
    String selectedDate = "";

    mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {

            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            selectedDate = sdf.format(new Date(mCalendarView.getDate()));
        }
    });
    btnConfirmCal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                ScheduleList scheduleList = new ScheduleList(et_assignName.getText().toString(),
                        et_assignDesc.getText().toString(), selectedDate);
                Toast.makeText(CalendarActivity.this, scheduleList.toString(), Toast.LENGTH_SHORT).show();
            }
            catch (Exception e) {
                Toast.makeText(CalendarActivity.this, "Error creating schedule", Toast.LENGTH_SHORT).show();
            }

        }
    });

}
  • Related