从一个 Fragment 传递值到另一个 Fragment

huangapple 未分类评论49阅读模式
英文:

Pass values from a Fragment to another

问题

以下是已翻译的内容:

FirstFragment:

firebaseFirestore.collection("QuizList")
                .document(quizId).collection("Results")
                .document(currentUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if(task.isSuccessful()){
                    DocumentSnapshot result = task.getResult();

                    Long correct = result.getLong("correct");
                    Long wrong = result.getLong("wrong");
                    Long missed = result.getLong("unanswered");

                    resultCorrect.setText(correct.toString());
                    resultWrong.setText(wrong.toString());
                    resultMissed.setText(missed.toString());

                    //Calculate Progress
                    Long total = correct + wrong + missed;
                    Long percent = (correct*100)/total;

                    ListFragment listFragment = new ListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putLong("total", total);
                    bundle.putLong("percent", percent);
                    listFragment.setArguments(bundle);

                    resultPercent.setText(percent + "%");
                    resultProgress.setProgress(percent.intValue());
                }
            }
        });

SecondFragment:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle bundle = this.getArguments();
        if (bundle != null) {
            Long total = getArguments().getLong("total", 0);
            Long percent = getArguments().getLong("percent", 0);

            Toast.makeText(getActivity(), String.valueOf(total), Toast.LENGTH_SHORT).show();
        }
}

是否还有其他需要翻译的内容?

英文:

I have two fragments. The first one get data from firebase, and the second one use the data. I am setting the data bundle using the setArguments() in first fragment and consuming it using getArguments in the second fragment. But i get a null value when consuming it. The below are the fragments.

FirstFragment;

firebaseFirestore.collection(&quot;QuizList&quot;)
                .document(quizId).collection(&quot;Results&quot;)
                .document(currentUserId).get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
            @Override
            public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
                if(task.isSuccessful()){
                    DocumentSnapshot result = task.getResult();

                    Long correct = result.getLong(&quot;correct&quot;);
                    Long wrong = result.getLong(&quot;wrong&quot;);
                    Long missed = result.getLong(&quot;unanswered&quot;);

                    resultCorrect.setText(correct.toString());
                    resultWrong.setText(wrong.toString());
                    resultMissed.setText(missed.toString());

                    //Calculate Progress
                    Long total = correct + wrong + missed;
                    Long percent = (correct*100)/total;

                    ListFragment listFragment = new ListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putLong(&quot;total&quot;, total);
                    bundle.putLong(&quot;percent&quot;, percent);
                    listFragment.setArguments(bundle);

                    resultPercent.setText(percent + &quot;%&quot;);
                    resultProgress.setProgress(percent.intValue());
                }
            }
        }); 

SecondFragment:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

Bundle bundle = this.getArguments();
        if (bundle != null) {
        Long total = getArguments().getLong(&quot;total&quot;, 0);
        Long percent = getArguments().getLong(&quot;percent&quot;,0);


        Toast.makeText(getActivity(),String.valueOf(total),Toast.LENGTH_SHORT).show();
       
        }
}

any help to do that? thank you!

huangapple
  • 本文由 发表于 2020年5月2日 12:36:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/61554527.html
匿名

发表评论

匿名网友

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

确定