code-troopers / android-betterpickers

[NO LONGER MAINTAINED] Android library for better Picker DialogFragments
2.67k stars 563 forks source link

onDialogNumberSet of NumberPickerBuilder not reachable from custom ViewGroup #277

Open alexodus opened 8 years ago

alexodus commented 8 years ago

In my app I moved the view logic from activity to a custom viewgroup (that inherited from CoordinatorLayout). I have:

public class MyCustomView extends CoordinatorLayout implements NumberPickerDialogFragment.NumberPickerDialogHandlerV2 {
....
    private void myMethod() {
                final BigDecimal minNumberAllowed = new BigDecimal("1");
                NumberPickerBuilder npb = new NumberPickerBuilder()
                        .setFragmentManager(context.getSupportFragmentManager())
                        .setStyleResId(R.style.BetterPickersDialogFragment)
                        .setMinNumber(minNumberAllowed)
                        .setLabelText("Points");
                npb.show();
    }
@Override
    public void onDialogNumberSet(int reference, BigInteger number, double decimal, boolean isNegative, BigDecimal fullNumber) {
        logger.debug("-"); //<- never reached
    }
}

The problem inside your code is that: only activity and fragment are permitted. Why?

if (activity instanceof NumberPickerDialogHandlerV2) {
                    final NumberPickerDialogHandlerV2 act = (NumberPickerDialogHandlerV2) activity;
                    act.onDialogNumberSet(mReference, mPicker.getNumber(), mPicker.getDecimal(), mPicker.getIsNegative(), number);
                } else if (fragment instanceof NumberPickerDialogHandlerV2) {
                    final NumberPickerDialogHandlerV2 frag = (NumberPickerDialogHandlerV2) fragment;
                    frag.onDialogNumberSet(mReference, mPicker.getNumber(), mPicker.getDecimal(), mPicker.getIsNegative(), number);
                }

I suppose, IMHO, that this is a bad design of this component

fchauveau commented 8 years ago

Hello @alexodus The is problably an error in design of all component, but now it's our legacy :/

I think your CustomView is added a some point in a fragement or an activity. So your activity or fragment can implement NumberPickerDialogFragment.NumberPickerDialogHandlerV2 then passing this result to your customView.