小编Xar*_*ren的帖子

将数据从 BottomSheetDialogFragment 传递或侦听到 Fragment

我试图监听 BotomSheetDialogFragment 中的数据或将数据传递到 Fragment 中以更改 Fragment 上的某些内容(就像选择器一样)。

我尝试使用 getTargetFragment 实例化侦听器,但出现编译器错误 Found: 'MyFragment', required: 'android.support.v4.app.Fragment' less..

有什么想法还是我采取了错误的方法?

public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment implements View.OnClickListener {


ReportType reportType;

public interface OnChooseReasonListener {
    void onChooseReason(ReportType reportType);
}

OnChooseReasonListener listener;

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.picker_bottom_sheet_, null);
    dialog.setContentView(contentView);
    CoordinatorLayout.LayoutParams layoutParams =
            (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();

    //get null here!!!:
    listener = (OnChooseReasonListener)  getParentFragment();// or with getTargetFragment();
  }

  @Override
public void onClick(View view) { …
Run Code Online (Sandbox Code Playgroud)

android listener android-fragments

6
推荐指数
1
解决办法
6475
查看次数

安全地使用Enum valueOf进行交换机上的字符串比较

如果我得到的字符串不同于枚举上支持的字符串,如何安全地实现valueOf的使用ACTION.我的意思是ACTION.valueOf(valueToCompare)即使发生valueToCompare不是有效的枚举成员,也可以强制获取有效值

当我得到预期的执行valueToCompare"CRY""CRYALOT""cryalot"等.

我接受java.lang.IllegalArgumentException了代码中的案例.

public enum ACTION{
    CRY,
    CRYALOT;
}

public static void main(String[] args) {

    String valueTocompare = "posible not expected value".toUpperCase();

    switch (ACTION.valueOf(valueToCompare)) {
        case CRY:
            System.out.println("Cry");
            break;
        case CRYALOT:
            System.out.println("Cry a lot");
            break;
        default:
            System.out.println("catch posible not expected value");
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑和使用的解决方案:

我通过使用try-catch解决了这个问题,因为@Peter Lawrey建议:

public enum ACTION{
    CRY,
    CRYALOT,
    NOTVALID;
}

public static void main(String[] args) {

    String valueToCompare = "NOTVALID"; …
Run Code Online (Sandbox Code Playgroud)

java enums

4
推荐指数
2
解决办法
3057
查看次数

标签 统计

android ×1

android-fragments ×1

enums ×1

java ×1

listener ×1