[英]Android, how can i use callback from hellper method in controller?
我在控制器中对Helper方法进行了以下调用:
DialogHelper.showListDialog(R.string.select_recipient_please, recipientsArr, null, context);
和辅助方法:
public static void showListDialog(int title, String[] items, Drawable icon, Context ctx) {
new MaterialDialog.Builder(ctx)
.title(title)
.items(items)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
Logger.d("Selected");
Logger.d(String.valueOf(which));
/**
* If you use alwaysCallSingleChoiceCallback(), which is discussed below,
* returning false here won't allow the newly selected radio button to actually be selected.
**/
return true;
}
})
.positiveText("Select")
.show();
}
而且我想在控制器而不是帮助程序中处理重写的onSelection方法。
我如何以正确的方式来做? 我应该为此使用界面吗?
非常感谢您的任何建议或示例。
编辑:不适用于我的附加示例
控制器方式:
public void showRecipientsPicker(ArrayList<String> recipents){
try {
String[] recipientsArr = new String[recipents.size()];
recipientsArr = recipents.toArray(recipientsArr);
DialogHelper dh = new DialogHelper(context);
dh.showListDialog(R.string.select_recipient_please, recipientsArr, null, new DialogHelperListener() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
Logger.d("TEST");
Logger.d(String.valueOf(which));
}
});
} catch (Exception e){
Logger.d(e.getMessage());
toastHelper.showToast(R.string.cannot_show_recipients,
Constants.Global.TOAST_DURATION_MEDIUM);
TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
Constants.ExceptionMessage.EXC_CANNOT_SHOW_RECIPIENT_LIST, true);
}
}
接口类:
public interface DialogHelperListener {
void onSelection(MaterialDialog dialog, View view, int which, CharSequence text);
}
助手类:
public class DialogHelper {
private Context mCtx;
public DialogHelper(Context ctx) {
mCtx = ctx;
}
/**
* Creating a list dialog only requires passing in an array of strings
*/
public void showListDialog(int title, String[] items, Drawable icon, DialogHelperListener callback) {
new MaterialDialog.Builder(this.mCtx)
.title(title)
.items(items)
.itemsCallbackSingleChoice(-1, (MaterialDialog.ListCallbackSingleChoice) callback)
.positiveText("Select")
.show();
}
}
它引发以下异常:
controller.MessageController$2 cannot be cast to com.afollestad.materialdialogs.MaterialDialog$ListCallbackSingleChoice
您可以将回调接口添加为函数的参数,因此如下所示:
public static void showListDialog(int title, String[] items, Drawable icon, Context ctx, MaterialDialog.ListCallbackSingleChoice callback) {
new MaterialDialog.Builder(ctx)
.title(title)
.items(items)
.itemsCallbackSingleChoice(-1, callback)
.positiveText("Select")
.show();
}
然后,在您的控制器中,您可以实现以下行为:
showListDialog(title, items, icon, ctx, new MaterialDialog.ListCallbackSingleChoice(){
@Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//Do something here
}
})
编辑
出问题的是,您的方法接受DialogHelperListener
然后尝试将其DialogHelperListener
为MaterialDialog.ListCallbackSingleChoice
。 现在,我假设您要对控制器代码使用DialogHelperListener
接口。 您不能简单地进行强制转换,您需要实现MaterialDialog.ListCallbackSingleChoice
并调用您自己的接口,如下所示:
public void showListDialog(int title, String[] items, Drawable icon, DialogHelperListener callback) {
new MaterialDialog.Builder(this.mCtx)
.title(title)
.items(items)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice(){
@Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
callback.onSelection(dialog, view, which);
}
})
.positiveText("Select")
.show();
}
虽然我不确定为什么您要使用直接模仿MaterialDialog.ListCallbackSingleChoice
接口的接口
我如何以正确的方式来做? 我应该为此使用界面吗?
是的,我想您将需要使用Interface并根据事件接收回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.