gpt4 book ai didi

java - Android,我如何在 Controller 中使用 hellper 方法的回调?

转载 作者:行者123 更新时间:2023-12-01 18:11:14 24 4
gpt4 key购买 nike

我在 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();
}

我想在 Controller 而不是助手中处理重写的 onSelection 方法。

请问我该如何以正确的方式做到这一点?我应该为此使用接口(interface)吗?

非常感谢您的任何建议或示例。

编辑:添加了对我不起作用的示例

Controller 方法:

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);

}
}

接口(interface)类:

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

最佳答案

您可以将回调接口(interface)作为参数添加到函数中,如下所示:

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();
}

然后在您的 Controller 中,您可以实现此行为:

showListDialog(title, items, icon, ctx, new MaterialDialog.ListCallbackSingleChoice(){
@Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//Do something here
}
})

编辑

这里出现的问题是您的方法接受 DialogHelperListener 并且您尝试将其转换为 MaterialDialog.ListCallbackSingleChoice。现在我假设您想要为 Controller 代码使用 DialogHelperListener 接口(interface)。您不能简单地进行转换,您需要实现 MaterialDialog.ListCallbackSingleChoice 并调用您自己的接口(interface),如下所示:

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 界面的界面

关于java - Android,我如何在 Controller 中使用 hellper 方法的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32843506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com