0

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;

    }
user3155478
  • 975
  • 1
  • 10
  • 16
  • "I wonder if its possible to change the code so that it becomes a member-function of MainActivity instead?" -- what code? The whole `Fragment`? – CommonsWare Aug 06 '14 at 20:35
  • It implements a callback interface, so it seems unlikely. Otherwise you'd be able to do it using an anonymous subclass of DialogFragment (although not sure why you would want to do that either). – EpicPandaForce Aug 06 '14 at 20:41
  • @Zhuinden the problem is that I want to set a textview in the MainActivity from a callback from this class (not shown here). But since the callback-funcktion residing in MainActivity also has to be static - i cannot set the textview in the MainActivity – user3155478 Aug 06 '14 at 20:45
  • 1
    ...the callback function static? What? You're going to need to post more code, because I'm not sure what you mean, but it sure sounds wrong. – EpicPandaForce Aug 06 '14 at 20:46
  • Describe exactly what you want to happen and not how you think it should happen. – beplaya Aug 06 '14 at 20:49
  • @beplaya - update made – user3155478 Aug 06 '14 at 20:59
  • @user3155478 Read this: http://developer.android.com/training/basics/fragments/communicating.html – Kevin Coppock Aug 06 '14 at 21:07

0 Answers0