我正在嘗試將MainActivity的值發送到HomeFragment。我創建了一個帶有底部導航的新項目,我在主活動中刪除了來自firebase的數據,我需要在刪除了來自firebase的數據后將該數據發送到HomeFragment。我用這個包,但我做不到。我怎樣才能解決這個問題?
Thanks
這是我的主要活動
public class MainActivity extends AppCompatActivity {
String age= "12";
String ?ifre;
ArrayList<String> list=new ArrayList<>();
FirebaseFirestore firebaseFirestore;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseApp.initializeApp(MainActivity.this);
firebaseFirestore=FirebaseFirestore.getInstance();
getDatafromFireBase();
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
public String getMyData(){
return ?ifre;
}
public void getDatafromFireBase(){
DocumentReference documentReference=(DocumentReference) firebaseFirestore.collection("?ifre").document("?ifre");
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
if (error!=null){
}
if (value!=null){
Map<String,Object> map=value.getData();
Number ?ifre1=(Number) value.get("?ifre");
?ifre=String.valueOf(?ifre1);
HomeFragment homeFragment=new HomeFragment();
System.out.println(?ifre+"98");
Bundle bundle=new Bundle();
bundle.putString("?ifre",?ifre);
homeFragment.setArguments(bundle);
}
}
});
}
}
這是我的家庭片段
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
String name;
ArrayList<String> list2;
String ?ifre;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list2=new ArrayList<>();
FirebaseApp.initializeApp(getContext());
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
textView.setText(name);
String deneme = getArguments().getString("?ifre");
textView.setText(deneme);
System.out.println(deneme);
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
如果已經創建了片段,
onCreateView()
方法在附加片段時只被調用一次,那么在從活動接收數據時,getArgumens()
不會被調用。所以在本例中,我建議您使用ViewModel來與片段通信。
首先,您需要在buid.gradel中添加這個Dependencie:
然后創建一個擴展ViewModel的類:
在活動中初始化
onCreate()
方法中的ViewModel:當您發送數據時,請按以下方式使用:
最后,要從片段中接收數據,請使用如下觀察者:
Update
要發送
ArrayList<String>
,ViewModel類應如下所示:然后要從活動中發送此列表,只需執行以下操作:
最后在你的片段中收到這個列表:
更多信息請查閱官方文件