Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has been answered just fine, but I keep finding this answer from Google and I wanted to share a non-anonymous class solution. I prefer reusable classes myself and may be helpful to others.</p> <p>In this example, I'm using a <code>DialogFragment</code> implementation and retrieving a value via a <em>callback</em> method.</p> <p>The <em>callback</em> method to get values from a <em>Dialog</em> can be done by creating a public <strong>interface</strong></p> <pre><code>public interface OnDialogSelectorListener { public void onSelectedOption(int selectedIndex); } </code></pre> <p>Also the <code>DialogFragment</code> implements <code>DialogInterface.OnClickListener</code> which means you can register the class you've implemented as the <em>OnClickListener</em> for the <code>DialogFragment</code> that is being created.</p> <p>For example</p> <pre><code>public Dialog onCreateDialog(Bundle savedInstanceState) { final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); builder.setTitle(R.string.select); builder.setSingleChoiceItems(mResourceArray, mSelectedIndex, this); builder.setPositiveButton(R.string.ok, this); builder.setNegativeButton(R.string.cancel, this); return builder.create(); } </code></pre> <p>The line</p> <p><code>builder.setSingleChoiceItems(mResourceArray, mSelectedIndex, this);</code></p> <p>Creates a choice dialog with the options from a <em>resource array</em> stored in <em>mResourceArray</em>. This also preselects an option index from what is stored in <em>mSelectedIndex</em> and finally it sets <code>this</code> itself as the <em>OnClickListener</em>. (See full code at the end if this paragraph is a tad confusing)</p> <p>Now, the <em>OnClick</em> method is where you grab the value that comes from the dialog</p> <pre><code>@Override public void onClick(DialogInterface dialog, int which) { switch (which) { case Dialog.BUTTON_NEGATIVE: // Cancel button selected, do nothing dialog.cancel(); break; case Dialog.BUTTON_POSITIVE: // OK button selected, send the data back dialog.dismiss(); // message selected value to registered callbacks with the // selected value. mDialogSelectorCallback.onSelectedOption(mSelectedIndex); break; default: // choice item selected // store the new selected value in the static variable mSelectedIndex = which; break; } } </code></pre> <p>What happens here is when an item is selected, it's stored in a variable. If the user clicks the <em>Cancel</em> button, no update is sent back and nothing changes. If the user clicks the OK button, it returns the value to the <code>Activity</code> that created it via the <em>callback</em> created.</p> <p>As an example, here is how you would create the dialog from a <code>FragmentActivity</code>.</p> <pre><code>final SelectorDialog sd = SelectorDialog.newInstance(R.array.selector_array, preSelectedValue); sd.show(getSupportFragmentManager(), TAG); </code></pre> <p>Here, the resource array _R.array.selector_array_ is an array of strings to show in the dialog and <em>preSelectedValue</em> is the index to select on open.</p> <p>Finally, your <code>FragmentActivity</code> will implement <code>OnDialogSelectorListener</code> and will receive the callback message.</p> <pre><code>public class MyActivity extends FragmentActivity implements OnDialogSelectorListener { // .... public void onSelectedOption(int selectedIndex) { // do something with the newly selected index } } </code></pre> <p>I hope this is helpful to someone, as it took me MANY attempts to understand it. A full implementation of this type of <code>DialogFragment</code> with a <em>callback</em> is here.</p> <pre><code>public class SelectorDialog extends DialogFragment implements OnClickListener { static final String TAG = "SelectorDialog"; static int mResourceArray; static int mSelectedIndex; static OnDialogSelectorListener mDialogSelectorCallback; public interface OnDialogSelectorListener { public void onSelectedOption(int dialogId); } public static DialogSelectorDialog newInstance(int res, int selected) { final DialogSelectorDialog dialog = new DialogSelectorDialog(); mResourceArray = res; mSelectedIndex = selected; return dialog; } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mDialogSelectorCallback = (OnDialogSelectorListener)activity; } catch (final ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnDialogSelectorListener"); } } public Dialog onCreateDialog(Bundle savedInstanceState) { final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); builder.setTitle(R.string.select); builder.setSingleChoiceItems(mResourceArray, mSelectedIndex, this); builder.setPositiveButton(R.string.ok, this); builder.setNegativeButton(R.string.cancel, this); return builder.create(); } @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case Dialog.BUTTON_NEGATIVE: dialog.cancel(); break; case Dialog.BUTTON_POSITIVE: dialog.dismiss(); // message selected value to registered calbacks mDialogSelectorCallback.onSelectedOption(mSelectedIndex); break; default: // choice selected click mSelectedIndex = which; break; } } } </code></pre> <p><strong>Question from a comment</strong> How to call this from a <code>Fragment</code> instead of an <code>Activity</code>.</p> <p>First make a few changes to the <code>DialogFragment</code>.</p> <p>Remove the <code>onAttach</code> event since that's not the easiest way in this scenario.</p> <p>Add a new method to add a reference to the callback</p> <pre><code>public void setDialogSelectorListener (OnDialogSelectorListener listener) { this.mListener = listener; } </code></pre> <p>Implement the listener in your <code>Fragment</code></p> <pre><code>public class MyFragment extends Fragment implements SelectorDialog.OnDialogSelectorListener { // .... public void onSelectedOption(int selectedIndex) { // do something with the newly selected index } } </code></pre> <p>Now create a new instance and pass in a reference to the <code>Fragment</code> to use it.</p> <pre><code>final SelectorDialog sd = SelectorDialog.newInstance(R.array.selector_array, preSelectedValue); // this is a reference to MyFragment sd.setDialogSelectorListener(this); // mActivity is just a reference to the activity attached to MyFragment sd.show(this.mActivity.getSupportFragmentManager(), TAG); </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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