S
S
SimpleName2019-03-25 23:43:49
Java
SimpleName, 2019-03-25 23:43:49

How to pass a value from another fragment through the ViewModel in database.getReference().child() of a fragment?

It is necessary to pass a word to the fragment, depending on which firebase should load the data. How to do it? How to load string into database.getReference().child() passed through ViewModel?
SharedViewModel Code

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;
    }
}

Fragment1 code that sends a string to firebase when the image is clicked.
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 snippet, which contains a list that should load data from firebase based on the received string (the code has been 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;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SimpleName, 2019-03-28
@SimpleName

in line
should be replaced String.valueOf(model.getSelected())withmodel.getSelected().getValue()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question