简体   繁体   中英

Android Alert Dialog is not showing

I'm comparing the strings, if all are equal it has to show a dialog "ALL ARE EQUAL" else another dialog "NOT EQUAL". I want to use only OK button in Alert Dialog. And my code:

if(s1.equals("yes") && s2.equals("yes") && s3.equals("yes") && s4.equals("yes"))
        showA();

Where the showA() method is

private void showA() {
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setTitle("Hello!!");
    ab.setMessage("ALL ARE EQUAL");
    ab.setCancelable(false);
    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });
    AlertDialog ad = ab.create();

    ad.show();

}

If it doesn't go inside method, I think your string might be "YES" or "Yes".

Why don't you try it as shown below?

if(s1.equalsIgnoreCase("yes") && s2.equalsIgnoreCase("yes") && s3.equalsIgnoreCase("yes") && s4.equalsIgnoreCase("yes"))
    showA();

Seem your string s1,s2,s3 &s s4 is in undefined case, better to use equalsIgnoreCase like below:

s1.equalsIgnoreCase("yes")

instead

s1.equals("yes")

if(s1.equalsIgnoreCase("yes") && s2.equalsIgnoreCase("yes") && s3.equalsIgnoreCase("yes") && s4.equalsIgnoreCase("yes"))      {
        showA();
} else {
     showB()
}

private void showA() {
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setTitle("Hello!!");
    ab.setMessage("ALL ARE EQUAL");
    ab.setCancelable(false);
    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });
    AlertDialog ad = ab.create();

    ad.show();    
}

private void showB() {
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setTitle("Hello!!");
    ab.setMessage("NOT EQUAL");
    ab.setCancelable(false);
    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });
    AlertDialog ad = ab.create();

    ad.show();   
}

You require something like this.

Your Code works absolutely fine. Just make sure your strings are equal to yes Case-Sensitive.

First of all you have to check value of s1,s2,s3,s4 and then compare.

your code is right,.

if(s1.equalsIgnoreCase("yes") && s2.equalsIgnoreCase("yes") && s3.equalsIgnoreCase("yes") && 

s4.equalsIgnoreCase("yes"))      {


       showAlertDialog("All ARE EQUAL");

} else {

     showAlertDialog("All ARE NOT EQUAL");
}



private void showAlertDialog(String msg){

    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setTitle("Hello!!");
    ab.setMessage(msg);
    ab.setCancelable(false);
    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });
    AlertDialog ad = ab.create();

    ad.show();    

}

Don't put the same code twice, just create a method and pass the parameter which you require.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM