How to send data to an AlertDialog [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-23 01:42:15

问题


I'm want to send some data to an altert dialog, to know identify winner player in my game.

Here is the code in my MainActivity.java:

if (game.comprobarCuatro(Game.JUGADOR)) {
            dibujarTablero();
            resultadoTextView.setText(R.string.gana_humano);
            new AlertDialogFragment().show(getFragmentManager(),"ALERT DIALOG");

            return;
        }

And here is my code in AlertDialogFragment.java, where I want to display a different Tittle, according to the received input.

public class AlertDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final MainActivity main = (MainActivity) getActivity();

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                getActivity());
        alertDialogBuilder.setTitle(R.string.fin_del_juego);
        alertDialogBuilder.setMessage(R.string.fin_message);
        alertDialogBuilder.setPositiveButton("Si",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        main.game.restart();
                        main.dibujarTablero();
                        dialog.dismiss();
                    }
                });
        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        main.finish();
                        dialog.dismiss();
                    }
                });
        return alertDialogBuilder.create();
    }
}

My question is, how do I send that information to the dialog to show in the title a different message each time? Is it possible to send information to an alert dialog?

Doing some research somebody suggested to use save it in the preferences and read it later, which I don't think is a good solution.

Thank you!


回答1:


It's very easy. like call from Activity like

AlertDialogFragment dFragment = new AlertDialogFragment ().newInstance("your Message");
dFragment.show(getSupportFragmentManager(), "Frag");

Now in your AlertDialogFragment do like

 public static AlertDialogFragment newInstance(String msg) {
      AlertDialogFragment fragment = new AlertDialogFragment();

        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        fragment.setArguments(bundle);

        return fragment;
    }

and then get this msg in onCreateDialog(..) like

 String msg= getArguments().getString("msg");

Code:

public class AlertDialogFragment extends DialogFragment {

  public static AlertDialogFragment newInstance(String msg) {
      AlertDialogFragment fragment = new AlertDialogFragment();

        Bundle bundle = new Bundle();
        bundle.putString("msg", msg); // set msg here
        fragment.setArguments(bundle);

        return fragment;
    }

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final MainActivity main = (MainActivity) getActivity();

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            getActivity());
    alertDialogBuilder.setTitle(R.string.fin_del_juego);
    alertDialogBuilder.setMessage(getArguments().getString("msg"));//get Mesg here
    alertDialogBuilder.setPositiveButton("Si",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    main.game.restart();
                    main.dibujarTablero();
                    dialog.dismiss();
                }
            });
    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    main.finish();
                    dialog.dismiss();
                }
            });
    return alertDialogBuilder.create();
}
}


来源:https://stackoverflow.com/questions/29254001/how-to-send-data-to-an-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!