从Activity传递数据给Fragment。

huangapple go评论74阅读模式
英文:

Pass data from an Activity to a fragment

问题

我在这方面有点迷茫,所以我已经将数据从我的LoginActivity传递到了DashboardActivity。现在它可以正常工作了(谢天谢地)。但是现在,我想将那些数据传递给一个片段。

我在我的DashboardActivity中有以下代码:

Intent get = getIntent();
String userId = get.getStringExtra(LoginActivity.EXTRA_ID);

Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);

我在我的片段中有以下代码:

Bundle bundle = getArguments();
String userId = bundle.getString("userId");
int uid = Integer.parseInt(userId);

是这样吗?

英文:

I am a little bit lost in this, so I already passed data from my LoginActivity to my DashboardActivity. And it works now (thank god). But now, I would love to pass that data to a fragment.

I have this on my DashboardActivity:

Intent get = getIntent();
String userId = get.getStringExtra(LoginActivity.EXTRA_ID);

Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);

I have this on my Fragment:

Bundle bundle = getArguments();
String userId = bundle.getString("userId");
int uid = Integer.parseInt(userId);

Is it like this?

答案1

得分: 1

获取数据在Fragment和Activity中的方式不同。
要向Fragment发送额外数据,可以按照以下方式进行操作。

在你的Activity类中

Bundle bundle = new Bundle();
bundle.putString("userId", "要发送的值");

FragmentClass frag = new FragmentClass();
frag.setArguments(bundle);

然后在Fragment的onCreateView方法中

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("userId");    
    return inflater.inflate(R.layout.fragment, container, false);
}

你代码中的问题是你使用了错误的id来获取参数。

正确的方式是

String userId = getArguments().getString("userId");
英文:

Getting data in Fragment and Activity are not done in the same way.
To send extra to a fragment, You can do it this way.

In your Activity Class

Bundle bundle = new Bundle();
bundle.putString("userId", "value you want to send");

FragmentClass frag = new FragmentClass();
frag.setArguments(bundle);

Then in Fragement onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("userId");    
    return inflater.inflate(R.layout.fragment, container, false);
}

The issue with your code is you're getting the argument with a wrong id.

Right way

String userId = getArguments().getString("userId");

答案2

得分: 0

从活动到片段,您应该像下面这样使用bundle:

Bundle bundle = new Bundle();
bundle.putString(LoginActivity.EXTRA_ID, "附加值");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);

然后在片段中,在onCreate()方法中获取数据,例如:

Bundle bundle = this.getArguments();
if (bundle != null) {
    String extraId = bundle.getString(key, defaultValue);
}
英文:

From activity to fragment you should use bundle as below

Bundle bundle = new Bundle();
bundle.putString(LoginActivity.EXTRA_ID, "Extra value");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);

Then in your Fragment, get the data for example in onCreate() method

Bundle bundle = this.getArguments();
if (bundle != null) {
        String extraId = bundle.getString(key, defaultValue);
}

huangapple
  • 本文由 发表于 2020年6月5日 22:24:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/62217595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定