-1

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

}
cjds
  • 8,268
  • 10
  • 49
  • 84
DroidLearner
  • 2,115
  • 5
  • 31
  • 50

6 Answers6

10

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();
lukad
  • 17,287
  • 3
  • 36
  • 53
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
4

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")

RobinHood
  • 10,897
  • 4
  • 48
  • 97
2
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.

NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
Android
  • 3,828
  • 9
  • 46
  • 79
1

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

Zeeshan
  • 741
  • 1
  • 7
  • 21
0

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

your code is right,.

Harshid
  • 5,701
  • 4
  • 37
  • 50
0
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.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Ketan Mehta
  • 272
  • 1
  • 12