DialogFragment ( Add Listener )
Use select information from DialogFragment(Single Choice Dialog).
Do something when clicking Positive or Negative button in Dialog.
In that case, Activity needs to communicate with DialogFragment.
We would like to add Listener!
How to add Listener? With Bundle? That’s a good question.
How to add Listener
Answer is to override onAttach of DialogFragment.
Example
Dialog
This is almost all same as previous sample.
public class SingleChoiceDialogFragment extends DialogFragment { public static final String DATA = "items"; public static final String SELECTED = "selected"; private SelectionListener listener; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { this.listener = (SelectionListener)activity; } catch ( ClassCastException oops ) { oops.printStackTrace(); } } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle bundle = getArguments(); AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); dialog.setTitle("Please Select"); dialog.setPositiveButton("Cancel", new PositiveButtonClickListener()); List<String> list = (List<String>)bundle.get(DATA); int position = bundle.getInt(SELECTED); CharSequence[] cs = list.toArray(new CharSequence[list.size()]); dialog.setSingleChoiceItems(cs, position, selectItemListener); return dialog.create(); } class PositiveButtonClickListener implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } } OnClickListener selectItemListener = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // process if ( listener != null ) { listener.selectItem(which); } dialog.dismiss(); } }; }
Override onAttach and keep listener
Listener
public interface SelectionListener { public void selectItem ( int position ); }
Actvity
To use listener, we need to implements Listener
public class DialogTestActivity extends Activity implements SelectionListener { Button openButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_main); openButton = (Button)findViewById(R.id.button1); openButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager manager = getFragmentManager(); SingleChoiceDialogFragment dialog = new SingleChoiceDialogFragment(); Bundle bundle = new Bundle(); bundle.putStringArrayList(SingleChoiceDialogFragment.DATA, getItems()); // Require ArrayList bundle.putInt(SingleChoiceDialogFragment.SELECTED, 0); dialog.setArguments(bundle); dialog.show(manager, "Dialog"); } }); } private ArrayList<String> getItems() { ArrayList<String> ret_val = new ArrayList<String>(); ret_val.add("Mikasa"); ret_val.add("Crysta"); ret_val.add("Ani"); ret_val.add("Sasha"); ret_val.add("Yumiru"); return ret_val; } @Override public void selectItem(int position) { Log.d("Favorites", getItems().get(position)); } }
Hi there! Would you mind if I share your blog with my zynga group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Thanks
If you would like to improve your knowledge only keep visiting this website and be updated with the newest news update
posted here.