IF语句返回2个AlertDialog而不是1

时间:2017-11-22 10:20:28

标签: java android if-statement alertdialog

我正在为我的应用添加验证,以确保最终用户不会尽可能地崩溃它。从下面的代码中你可以看到我正在检查2个用户条目.. spinnerselection 1和spinnerselection 2.基本上我添加了一个空检查,但后来我得到一个错误,说第二个if语句返回null。然后我将null检查和对话框添加到第二个IF语句中,它正在工作。

然而,alertdialog框弹出两次,因为它检查null两次,我只希望它出现一次..

我的IF语句设置不正确还是我可以更改它们以允许我只显示1个警告对话框?

REFRESH BUTTON:

 Button RefreshS1 = (Button) findViewById(R.id.refreshS1);
    RefreshS1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (spinnerSelection == null || spinnerSelection2 == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
                builder.setTitle("Data Input Error");
                builder.setMessage("Line S1 Data has not been entered yet!");
                builder.setIcon(R.drawable.warning_icon);
                builder.setCancelable(true);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            } else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("20")) {
                if (actualshippersnum > optimum20) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(69, 173, 88));
                    PB4.setMax(100);
                    PB4.setProgress(diffplus20);
                } else if (actualshippersnum < optimum20) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(242, 33, 33));
                    PB4.setMax(100);
                    PB4.setProgress(diffminus20);
                } else if (actualshippersnum == optimum20) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(28, 78, 173));
                    PB4.setMax(100);
                    PB4.setProgress(percentageActual20);
                }
            }

            if (spinnerSelection == null || spinnerSelection2 == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
                builder.setTitle("Data Input Error");
                builder.setMessage("Line S1 Data has not been entered yet!");
                builder.setIcon(R.drawable.warning_icon);
                builder.setCancelable(true);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            } else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("30")) {
                if (actualshippersnum > optimum30) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(69, 173, 88));
                    PB4.setMax(100);
                    PB4.setProgress(diffplus30);
                } else if (actualshippersnum < optimum30) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(242, 33, 33));
                    PB4.setMax(100);
                    PB4.setProgress(diffminus30);
                } else if (actualshippersnum == optimum30) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(28, 78, 173));
                    PB4.setMax(100);
                    PB4.setProgress(percentageActual30);
                }
            }
        }
    });

2 个答案:

答案 0 :(得分:2)

我认为这可以解决你的问题。

if (spinnerSelection == null || spinnerSelection2 == null) {
    ...
} else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("20")) {
    ...
} else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("30")) {
    ...
}

只需合并这样的if语句即可。甚至更好:

if (spinnerSelection == null || spinnerSelection2 == null) {
    ...
} else if (spinnerSelection.equals("S1") {
    if (spinnerSelection2.equals("20")) {
        ...
    } else if (spinnerSelection2.equals("30")) {
        ...
    }
}

答案 1 :(得分:0)

在你的第一个else上存在一个逻辑问题 - 如果你正在调用尚未初始化的对象的方法:spinnerSelection2.equals(&#34; 20&#34;)。您声称您打算只检查第一个null,但如果是,则检查从未初始化的值状态:

else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("20")) {
                if (actualshippersnum > optimum20) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(69, 173, 88));
                    PB4.setMax(100);
                    PB4.setProgress(diffplus20);
                } else if (actualshippersnum < optimum20) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(242, 33, 33));
                    PB4.setMax(100);
                    PB4.setProgress(diffminus20);
                } else if (actualshippersnum == optimum20) {
                    PB4 = findViewById(R.id.roundCornerProgressBar4);
                    PB4.setProgressColor(Color.rgb(28, 78, 173));
                    PB4.setMax(100);
                    PB4.setProgress(percentageActual20);
                }
            }