As in the database.getReference().child() snippet to pass a value from another fragment using ViewModel?

Need the fragment to the floor, depending on which firebase needs to load the data. How to do it? How to download a string in the database.getReference().child () is transmitted through the ViewModel?
Code SharedViewModel
public class SharedViewModel extends ViewModel {
 private MutableLiveData<String> selected = new MutableLiveData<>();

 public void select(String item) {
selected.setValue(item);
}

 public LiveData<String> getSelected() {
 return selected;
}
}

Фрагмента1 code that sends the string to firebase while you click on the picture.
public class Fragment1 extends Fragment {

 private SharedViewModel model;


 public Fragment1() {
 // Required empty public constructor
}


@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {

 View View = inflater.inflate(R. layout.fragment1, container, false);

 model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);

 ImageView imageView1 = view.findViewById(R. id.imageCompany);
 imageView1.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {

model.select("DataOne");

 Fragment2 newFragment = new Fragment2();
 ((AppCompatActivity) view.getContext()).getSupportFragmentManager().beginTransaction()
 .replace(R. id.content_frame, newFragment)
.addToBackStack(null)
.commit();
}
});

 return view;
}
}

Here is the second fragment, which is a list that needs to load loads data from firebase based on the string(code simplified).
public class Fragment2 extends Fragment {

 private DatabaseReference mRef;
 private FirebaseDatabase database;

 private SharedViewModel model;


 public Fragment2() {
 // Required empty public constructor
}

@Override
 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 // Inflate the layout for this fragment
 View View = inflater.inflate(R. layout.fragment2, container, false);

 final SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);

 model.getSelected().observe(this, new Observer() {
@Override
 public void onChanged(@Nullable Object o) {

}
});

 database = FirebaseDatabase.getInstance();
 mRef = database.getReference().child(String.valueOf(model.getSelected()));

 mRef.addChildEventListener(new ChildEventListener() {
@Override
 public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
 GalleryGridObject image = dataSnapshot.getValue(GalleryGridObject.class);
galleryList.add(image);
recyclerView.setAdapter(mAdapter);
}

@Override
 public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}
});

 return view;
}
}
March 19th 20 at 08:34
1 answer
March 19th 20 at 08:36
Solution
in line
mRef = database.getReference().child(String.valueOf(model.getSelected()));

need to replace the String.valueOf(model.getSelected()) on the model.getSelected().getValue()

Find more questions by tags FirebaseMaterial DesignAndroidJava