Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send data from DialogFragment to a Fragment?
    primarykey
    data
    text
    <p>I have a fragment that opens a <code>Dialogfragment</code> to get user input (a string, and an integer). How do I send these two things back to the fragment?</p> <p>Here is my DialogFragment:</p> <pre><code>public class DatePickerFragment extends DialogFragment { String Month; int Year; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().setTitle(getString(R.string.Date_Picker)); View v = inflater.inflate(R.layout.date_picker_dialog, container, false); Spinner months = (Spinner) v.findViewById(R.id.months_spinner); ArrayAdapter&lt;CharSequence&gt; monthadapter = ArrayAdapter.createFromResource(getActivity(), R.array.Months, R.layout.picker_row); months.setAdapter(monthadapter); months.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView&lt;?&gt; parentView, View selectedItemView, int monthplace, long id) { Month = Integer.toString(monthplace); } public void onNothingSelected(AdapterView&lt;?&gt; parent) { } }); Spinner years = (Spinner) v.findViewById(R.id.years_spinner); ArrayAdapter&lt;CharSequence&gt; yearadapter = ArrayAdapter.createFromResource(getActivity(), R.array.Years, R.layout.picker_row); years.setAdapter(yearadapter); years.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView&lt;?&gt; parentView, View selectedItemView, int yearplace, long id) { if (yearplace == 0){ Year = 2012; }if (yearplace == 1){ Year = 2013; }if (yearplace == 2){ Year = 2014; } } public void onNothingSelected(AdapterView&lt;?&gt; parent) {} }); Button button = (Button) v.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { getDialog().dismiss(); } }); return v; } } </code></pre> <p>I need to send the data after the button click and before <code>getDialog().dismiss()</code></p> <p>Here is the fragment that data needs to be sent to:</p> <pre><code>public class CalendarFragment extends Fragment { int Year; String Month; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); int position = getArguments().getInt("position"); String[] categories = getResources().getStringArray(R.array.categories); getActivity().getActionBar().setTitle(categories[position]); View v = inflater.inflate(R.layout.calendar_fragment_layout, container, false); final Calendar c = Calendar.getInstance(); SimpleDateFormat month_date = new SimpleDateFormat("MMMMMMMMM"); Month = month_date.format(c.getTime()); Year = c.get(Calendar.YEAR); Button button = (Button) v.findViewById(R.id.button); button.setText(Month + " "+ Year); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new DatePickerFragment().show(getFragmentManager(), "MyProgressDialog"); } }); return v; } } </code></pre> <p>so once the user selects a date in the <code>Dialogfragment</code>, it must return the month and year. </p> <p>Then, the text on the button should change to the month and year specified by user.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload