我有两个片段。第一个从Firebase获取数据,第二个使用数据。我在第一个片段中使用setArguments()设置数据束,并在第二个片段中使用getArguments使用它。但是我消费时会得到一个空值。以下是片段。
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();
}
}
有什么帮助吗?谢谢!