如何从一个片段传递数据给dialogFragment片段、数据、dialogFragment

2023-09-13 01:30:11 作者:怪峩冭天眞

我知道,这是问过,但我不很明白如何实现它。 我有一个片段myFragment中,我创建了一个myDialogueFragment的对象。 我想一定值传递给myDialogueFragment当我从myFragment调用它。 我有一个整数NUM,我想传递给myDialogueFragment和存储号码在本地数据库以及来自myDialogueFragment一些其他的信息。

I know that this was asked before, but I dont quite understand how to implement it. I have a fragment "myFragment" in which I create an object of a "myDialogueFragment". I want to pass some value to the myDialogueFragment when I invoke it from the myFragment. I have an integer num, that I want to pass to the myDialogueFragment and store that number in a local database along with some other info from the myDialogueFragment.

我可能是错了,但所有的code我所看到的是有关从myDialogueFragment将数据发送回,这是不是我真正想要的myFragment。

I might be mistaken, but all the code I have seen is about sending data from the myDialogueFragment back to the myFragment which is not what I really want.

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 void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNum = getArguments().getInt("num");
    ...
}

所以,这code得到myFragment的onCreate()方法中的参数。我想发送的参数myFragment(中)和接收他们在myDialogueFragment。

So, this code gets the arguments within the myFragment onCreate() method. I want to sent the arguments within the myFragment() and receive them in the myDialogueFragment.

我怎样才能做到这一点?

How can I achieve that?

推荐答案

您需要的是片段的setArguments如下:

What you need is to setArguments on the fragment as follows:

Bundle args = new Bundle();
args.putString("key", "value");
DialogFragment newFragment = new YourDialogFragment();  
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(), "TAG");

所有你现在要做的,就是赶上这些参数在你的片段,并用它们...

All you have to do now, is catch those arguments in your fragment and use them...

,并在对话框片段你读它像这样...

AND IN THE DIALOG FRAGMENT YOU READ IT LIKE THIS...

Bundle mArgs = getArguments();
String myValue = mArgs.getString("keyUsed to send it...");

商祺!

 
精彩推荐
图片推荐