简体   繁体   中英

Android, how can i use callback from hellper method in controller?

I have the following calling of the Helper method in the controller:

DialogHelper.showListDialog(R.string.select_recipient_please, recipientsArr, null, context);

And helper method:

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

And i would like to process overridden onSelection method in the Controller instead of the helper.

How can i do it in the right way please? Should I Use interface for this?

Many thanks for any advice or example.

EDIT: ADDED EXAMPLE WHICH IS NOT WORKING FOR ME

Controller method:

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 class:

public interface DialogHelperListener  {
    void onSelection(MaterialDialog dialog, View view, int which, CharSequence text);
}

Helper class:

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





}

It is throwing the following exception:

controller.MessageController$2 cannot be cast to com.afollestad.materialdialogs.MaterialDialog$ListCallbackSingleChoice

You could add the callback interface as an argument to the function, so something like this:

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

Then in your controller, you can implement this behaviour:

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

EDIT

The thing going wrong here is that your method accepts a DialogHelperListener and you try to cast it to MaterialDialog.ListCallbackSingleChoice . Now I am going to assume that you want to use the DialogHelperListener interface for your controller code. You cannot simply cast that, you would need to implement the MaterialDialog.ListCallbackSingleChoice and call your own interface, like this:

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

Though I am not sure why you would want to use an interface that directly mimics the MaterialDialog.ListCallbackSingleChoice interface

How can i do it in the right way please? Should I Use interface for this?

Yes, I guess you will need to use Interface and receive callbacks based on your events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM