Remend
Remend

Reputation: 93

Cannot create date picker dialog

I was using a class inside my activity in order to create a datepicker dialog and it was working until i migrated to android studio. Here is the class:

class StartDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // year, month, day από το onCreate
        DatePickerDialog dialog = new DatePickerDialog(Add_Expense.this, this, year, month, day);
        //DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth);
        return dialog;

    }
    @Override
    public void onDateSet(DatePicker view, int year, int month,``
            int day) {
        processDate(year, month, day);

    }
    private void processDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        int day_ofWeeks = cal.get(Calendar.DAY_OF_WEEK);
        showDate(year, month, day, day_ofWeeks);
    }
}


        public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new StartDatePicker();
    newFragment.show(getFragmentManager(), "start_date_picker");
}

The class StartDatePicker now raises an error: This fragment class should be public. After I declare it as public it raises another error: This fragment inner class should be static. I don't want to declare it as static because private void showDate(int year, int month, int day, int day_ofWeek) will not work.

Thank you in advance.

Upvotes: 1

Views: 518

Answers (1)

nPn
nPn

Reputation: 16728

There is probably a cleaner way to do this but, if you make StartDatePick public and static then you could do this:

in StartDatePicker create a field to hold a reference to your outer class

OuterClassType  mDateShower;

and create a setter for that field.

public void setDateShower(OuterClassType dateShower) {
    mDateShower = dateShower;
}

then replace your call to showDate(....); with mDateShower.showDate(...);

finally set the dateShower

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new StartDatePicker();
    newFragment.setDateShower(this);
    newFragment.show(getFragmentManager(), "start_date_picker");
}

Upvotes: 1

Related Questions