1

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.

1 Answer 1

2

Assuming DialogInterface.OnClickListener is a functional interface (i.e. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.