放入课堂时,警报对话框不显示

时间:2013-11-25 03:55:46

标签: android alertdialog

这是我的警告对话框类。

public class DialogCreator {

        public DialogCreator(Context context) {
            this.context = context;
        }

        private Context context;
        private int result = -1;
        public Type getResult() {
            switch(result) {
            case 0:
                return Type.QUEEN;
            case 1:
                return Type.KNIGHT;
            case 2:
                return Type.ROOK;
            case 3:
                return Type.BISHOP;
            }
            return null;
        }
        private Builder createDialog() {
            AlertDialog.Builder myPromotionDialog = new AlertDialog.Builder(
                    context, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
            myPromotionDialog.setTitle("Choose a piece to upgrade to...");
            String[] sortModeChoices = { "Queen", "Knight", "Rook", "Bishop"};
            myPromotionDialog.setSingleChoiceItems(sortModeChoices,
                    0, null);

            myPromotionDialog.setPositiveButton("Promote!",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            which = ((AlertDialog) dialog).getListView()
                                    .getCheckedItemPosition();
                            result = which;
                        }
                    });

            return myPromotionDialog;
        }

以下是我如何运行它:

DialogCreator whitePromotion = new DialogCreator(this);
                whitePromotion.createDialog().show();
                while (whitePromotion.getResult() == null) {}
                System.out.println(whitePromotion.getResult().toString());

对话框根本没有显示,我的代码卡在while循环中。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

while (whitePromotion.getResult() == null) {}这是你所处的循环,重新检查你的逻辑

result = -1;

switch包含0,1,2,3

的大小写

不要这样尝试,这将在getResult()中运行无限循环 Respond only when you got a点击

答案 1 :(得分:0)

Your not showing it up at all please replace this

DialogCreator whitePromotion = new DialogCreator(this);
                whitePromotion.createDialog().show();
                while (whitePromotion.getResult() == null) {}
                System.out.println(whitePromotion.getResult().toString());

with this one

DialogCreator whitePromotion = new DialogCreator(this);
                AlertDialog alertDialog = whitePromotion.createDialog();
                alertDialog.show();
                while (whitePromotion.getResult() == null) {}
                System.out.println(whitePromotion.getResult().toString());