Android,如何在控制器中使用来自hellper方法的回调?

时间:2015-09-29 12:01:32

标签: java android interface

我在控制器中调用了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 方法而不是帮助程序。

我怎么能以正确的方式做到这一点?我应该使用接口吗?

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

编辑:添加的示例不适用于我

控制器方法:

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

2 个答案:

答案 0 :(得分:3)

您可以将回调接口添加为函数的参数,如下所示:

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,并尝试将其强制转换为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界面的界面

答案 1 :(得分:1)

  

我怎么能以正确的方式做到这一点?我应该使用接口吗?   此?

是的,我想你需要使用Interface并根据你的事件接收回调。