您好我有以下代码:
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();
}
}
});
所以这里我有一个SAM表达式,在里面我想把((MainActivity) context).myDialog.setSingleChoiceItems
写成lambda。问题是setSingleChoiceItems
有3个参数,我不知道我应该如何将片段转换为lambda。它甚至可能吗?根据我的IDE,这就是我问这个问题的原因。
答案 0 :(得分:2)
假设DialogInterface.OnClickListener
是一个功能接口(即只有一个抽象方法):
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();
}
});
});
请注意,只有setSingleChoiceItems
的最后一个参数被转换为lambda表达式。