【问题标题】:Android DialogFragment newInstance optional parametersAndroid DialogFragment newInstance 可选参数
【发布时间】:2017-01-01 18:38:32
【问题描述】:

我一直在关注官方android developer docuthis one 在我的项目中使用DialogFragment。到目前为止一切顺利,因为我必须将数据传递给 DialogFragment 才能创建多选列表,所以我通过 newInstance 方法(传递项目)从 MainActivity 内部调用 DialogFragment,我得到了正确的结果。现在,我想传递另一个参数,也是 DialogFragment 的数据,但它必须是可选的,因为我不需要一直传递它。我有办法做到这一点吗?

编辑:
所以我从下面的 cmets 中获取了建议并创建了一个 setter 并将我希望传递给 DiagramFragment 的项目。它工作得很好,遗憾的是它并没有帮助我解决我的问题。我想传递第二个数据的原因是我想,如果用户打开 DialogFragment 并进行选择,然后重新打开 DialogFragment,他的最后一个选择就消失了。我想检查他已经以编程方式检查过的复选框,方法是将检查过的一次传回 DialogFragment,然后将正确的索引设置回mSelectedItems - 但即使索引设置正确,复选框也保持未选中状态..解决方法?

static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
}

...

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title
    builder.setTitle(R.string.pick_toppings)
    // Specify the list array, the items to be selected by default (null for none),
    // and the listener through which to receive callbacks when items are selected
           .setMultiChoiceItems(R.array.toppings, null,
                      new DialogInterface.OnMultiChoiceClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which,
                       boolean isChecked) {
                   if (isChecked) {
                       // If the user checked the item, add it to the selected items
                       mSelectedItems.add(which);
                   } else if (mSelectedItems.contains(which)) {
                       // Else, if the item is already in the array, remove it
                       mSelectedItems.remove(Integer.valueOf(which));
                   }
               }
           })
    // Set the action buttons
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK, so save the mSelectedItems results somewhere
                   // or return them to the component that opened the dialog
                   ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ...
               }
           });

    return builder.create();
}

【问题讨论】:

  • 也许是标准制定者
  • 只传递 null 怎么样?当它不可用时
  • @JawadLeWywadi 这可能吗?我将如何使用该 setter 函数 DialogFragment newFragment = MyDialogFragment.newInstance(items); 然后 newFragment.setMySetter(value); ?像这样??
  • @OlegBogdanov 将尝试一下。如果它有效,我很好
  • 好吧,我使用了您的两个建议并且它们都有效,但遗憾的是没有解决我的问题。我将编辑我的问题并提供更多详细信息..

标签: java android dialogfragment


【解决方案1】:

一个可选参数可以这样完成:

static MyDialogFragment newInstance() {
        return newInstance(-1);
}

static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
}

如果您没有号码,您可以拨打newInstance(),如果有,请拨打newInstance(300)

另一边:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     int num = getArguments().getInt("num");
     if(num == -1) {
       // no extra number
     } else {
       // do something with the extra number
     }

...

除了使用 -1 之外,您根本无法添加 Int 而只是检查默认值(我认为它在 API 文档中为 0)

【讨论】:

    【解决方案2】:

    我知道了,已经回答了 - 有趣的是我在谷歌上没有找到它..

    有一些最简单的方法可以做你想做的事,但让我们采用这种方法: 正如您所注意到的,setMultiChoiceItems() 方法接收一个 String[] 作为 DialogFragment 的项目和一个 boolean [] 来定义它们是否被选中。我们将使用这个数组来持久化我们的选择。此数组必须与 items 数组具有相同的长度,并且初始设置为 false。

    所以我所要做的就是创建一个 boolean[] itemsChecked 并将正确的索引设置为 true,以重新检查正确的复选框..

    原始问题Persist AlertDialog checked options - 全部归功于@joao2fast4u

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多