3

I have my Android studio set with these:

    classpath "me.tatarka:gradle-retrolambda:3.2.2"
    classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'

And I am trying to use lambdas to know what I can do or not.

When I did the following code:

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

The IDE grayed out the new DialogInterface.OnClickListener() telling me it could be replaced with a lambda. Nothing more or less. After looking into several examples. I tried things like:

    alertDialogBuilder.setPositiveButton("Okay", (DialogInterface dialog) -> {
            dialog.cancel();
    });

Also these:

alertDialogBuilder.setNegativeButton((DialogInterface) d -> d.cancel());

Among the errors:

Error:(99, 64) error: incompatible types: DialogInterface is not a functional interface multiple non-overriding abstract methods found in interface DialogInterface

How should I use lambda in this case?

DeMarco
  • 599
  • 1
  • 8
  • 26
  • 2
    This question may be interesting for you: http://stackoverflow.com/questions/23318109/is-it-possible-to-use-java-8-for-android-development – Henry Sep 17 '15 at 05:41

1 Answers1

11

onClick takes two parameters, and your example tries only show one. Be sure to include the which parameter in your lambda.

new AlertDialog.Builder(this).setPositiveButton("Okay", 
                                  (dialog, which) -> dialog.cancel());
iagreen
  • 31,470
  • 8
  • 76
  • 90