简体   繁体   中英

How to handle lambda expression with 3 parameters in Java8

Hello I have the following code :

 view.setOnLongClickListener((viewL) -> {
        final CharSequence[] optionsDialog = {"Edit", "Delete"};
        ((MainActivity) context).myDialog.setSingleChoiceItems(optionsDialog, 0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if(i==0){
                            final Intent myIntent = new Intent (context, MainInputActivity.class);
                            myIntent.putExtra("request", 8);
                            myIntent.putExtra("oldTask", mySubTask.getSubTaskText());
                            myIntent.putExtra("taskCode", mySubTask.getSubtaskCode());
                            ((MainActivity) context).startActivityForResult(myIntent, 8);
                            dialogInterface.dismiss();
                        }
                        if(i==1){
                            realm.executeTransaction((realm) -> mySubTask.deleteSubtask());
                            realm.refresh();
                            notifyDataSetChanged();
                            ((MainActivity) context).updateWidgets();
                            dialogInterface.dismiss();
                        }
                    }
                });

So here I have one SAM expression, and inside I want to write the ((MainActivity) context).myDialog.setSingleChoiceItems as lambda too. The problem is that setSingleChoiceItems have 3 parameters and I don't know how am I supposed to convert the snippet into lambda. Is it even possible ? According to my IDE it is, thats why I ask this question.

Assuming DialogInterface.OnClickListener is a functional interface (ie has only one abstract method) :

view.setOnLongClickListener((viewL) -> {
    final CharSequence[] optionsDialog = {"Edit", "Delete"};
    ((MainActivity) context).myDialog.setSingleChoiceItems(optionsDialog, 0,
            (DialogInterface dialogInterface, int i) -> {
                if(i==0){
                    final Intent myIntent = new Intent (context, MainInputActivity.class);
                    myIntent.putExtra("request", 8);
                    myIntent.putExtra("oldTask", mySubTask.getSubTaskText());
                    myIntent.putExtra("taskCode", mySubTask.getSubtaskCode());
                    ((MainActivity) context).startActivityForResult(myIntent, 8);
                    dialogInterface.dismiss();
                }
                if(i==1){
                    realm.executeTransaction((realm) -> mySubTask.deleteSubtask());
                    realm.refresh();
                    notifyDataSetChanged();
                    ((MainActivity) context).updateWidgets();
                    dialogInterface.dismiss();
                }     
            });
        });

Note that only the last argument of setSingleChoiceItems is converted to a lambda expression.

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