mercredi 22 novembre 2017

IF Statement returning 2 AlertDialogs instead of 1

I am adding validation to my app to ensure that the end user cannot crash it as much as possible. From the code below you can see that I am checking for 2 user entries.. spinnerselection 1 and spinnerselection 2. Basically I added one null check but then I got an error to say that the second if statement was returning a null. I then added the null check and dialog to the second IF statement as well and it is working.

However, the alertdialog box is popping up twice as it is checking for the null both times and I only want it to show up once..

Is my IF statements set up incorrect or can I change them to allow me to only show 1 alert dialog??

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);
                }
            }
        }
    });

Aucun commentaire:

Enregistrer un commentaire