DialogFragment - make it as a member of the outerclass instead of static inner class

505 Views Asked by At

this is a small example from Android tutorial showing a DialogFragment and DatePicker. Its implemented as a static inner class of the MainActivity. I wonder if its possible to change the code so that it becomes a member-function of MainActivity instead?

EDIT: the thing is that I want to set a textview in the MainActivity from this inner class where the DialogFragment is residing. But this inner class is static -in fact HAS to be static. ANd since its static, the callback that is made to main Activity - the callback function - also has to be static. But if this function is static - how can I access the textview in the MainActivity?

thanks!

TextView tv;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    tv = (TextView) findViewById(R.id.dateTextView);

 }


 public void showTimePickerDialog(View v) {
      newFragment = new DatePickerFragment();
      newFragment.show(getSupportFragmentManager(), "datePicker"); 
 }

 public static void notifyActivity() {
    // want to change the textview here but cannot since the method is static.
 }



 public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

    private int year, month, day;

    @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);


        DatePickerDialog date = new DatePickerDialog(getActivity(), DatePickerFragment.this, year, month, day);
        date.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        notifyActivity(); 
                    }
                });
        return date;

    }
0

There are 0 best solutions below