Android – retrieves and sets data from the dialog fragment (datepicker)

When I press the button on the activity, my dialogfragment class will be called. I want the date set through this dialogfragment to be displayed on the button text. Findviewbyid is rejected by the class

I found the similar question, but I couldn't relate it to my case

Code:

>Button to invoke the dialogue fragment:

public void datepicker(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker"); 
}

>Conversation fragment code:

public class DatePickerFragment extends DialogFragment implements
    DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceSateate) {

        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);

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen
        }
    }
}

resolvent:

Do this in the dialogfragment method that is responsible for receiving notification when the user sets the date

Button activityButton = (Button)getActivity().findViewById(R.id.myButton);
activityButton.setText (myDate);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>