私は、プロジェクト内でDialogFragmentを使用するために公式android developer docuとthis oneに従っています。 DialogFragmentにデータを渡してマルチチェックリストを作成する必要があるので、DialogFragmentをnewInstance
メソッド(アイテムを渡す)でMainActivityの中から呼び出し、正しい結果を得ています。さて、私はDialogFragmentのデータも別の引数を渡したいと思いますが、それはすべての時間を渡す必要はないのでオプションでなければなりません。これを達成する方法はありますか?Android DialogFragment newInstanceオプションのパラメータ
EDIT:
ので、私は以下のコメントからのアドバイスを取り、セッターを作成し、私はDiagramFragmentに渡すことを望んだアイテムを可決しました。それはうまく働いた、悲しいことに私の問題を解決するのに役立たなかった。私が2番目のデータを渡したかったのは、ユーザーが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();
}
標準joao2fast4u settersおそらく –
ちょうどnullを渡すのはどうですか?利用できない場合は –
@JawadLeWywadiが可能ですか?どうすればそのセッター関数を使用するのですか? 'DialogFragment newFragment = MyDialogFragment.newInstance(items);'それから 'newFragment.setMySetter(value);'?このような ?? – b101