2017-01-01 23 views
-2

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

编辑:
所以我把文章从下面的意见,并创造了一个二传手,并通过我希望传递给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(); 
} 
+1

标准也许 – 2017-01-01 18:46:39

+1

刚刚通过null如何?当它不可用 –

+0

@JawadLeWywadi可能吗?如何使用setter函数 'DialogFragment newFragment = MyDialogFragment.newInstance(items);'然后'newFragment.setMySetter(value);'?喜欢这个 ?? – b101

回答

3

一个可选参数,可以做这样的:

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,只是检查的默认值(我认为它0的API文档)

-2

我得到了它,它已经回答了 - 有趣,我没有发现它,当我GOOGLE ..

有最简单的方法做你想做的事,但让我们采取这种方法: 正如你已经注意到的,setMultiChoiceItems()方法接收一个String []作为你的DialogFragment的项目和一个布尔[]来定义它们是否被选中或者不。我们将使用这个数组来坚持我们的选择。该数组的长度必须与items数组的长度相同,并且最初将设置为false。

因此,所有我所要做的就是创建一个boolean[] itemsChecked并设置正确的指标为true,重新检查右复选框..

原始的问题Persist AlertDialog checked options - 所有功劳都@ joao2fast4u