Search code examples
androidandroid-fragmentsandroid-jetpackandroid-viewmodel

How to use ViewModel to update TextView after typing data in several EditTexts?


I have 3 questions each displayed in a fragment. After each question there is a fragment that has a TextView which has the answer that was typed in in the EditText of the previous questions fragment. I managed to achieve that the typed in data of the first question is displayed in the second fragment but when I go to the next question, in the EditText there is the data of the first question. How do I use the code shown here so the input of every question is only shown in the next fragment? (fragments: question 1 --> answer 1 --> question 2 --> answer 2 --> question 3 --> answer 3)

Here is the code of the first two fragments and the ModelView:

First Question Fragment:

public class FragmentQuestion1 extends Fragment {

private Button btnNavFrag1;
private EditText mEditTextQuestion1;
private SharedViewModel viewModel;

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

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

    btnNavFrag1 = view.findViewById(R.id.btn_question1);

    mEditTextQuestion1 = view.findViewById(R.id.edit_text_question_1);

    btnNavFrag1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            viewModel.getAnswer(1);

            ((GameActivity)getActivity()).setViewPager(2);

        }
    });

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    viewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
    viewModel.getAnswer(1).observe(this, new Observer<String>() {
        @Override
        public void onChanged(@Nullable String answer) {
            viewModel.setAnswer(1, answer);
            mEditTextQuestion1.setText(answer);
        }
    });
}

First Answer overview Fragment:

public class FragmentAnswer1 extends Fragment {

private View btnNavFragAns1;
private TextView tv_answer1;
private SharedViewModel viewModel;

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

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

    btnNavFragAns1 = view.findViewById(R.id.next_question_1);

    tv_answer1 = view.findViewById(R.id.answer_player_1_text_view);

    btnNavFragAns1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ((GameActivity)getActivity()).setViewPager(3);

        }
    });

    return view;
}

@Override
public void onActivityCreated(Bundle bundle) {
    super.onActivityCreated(bundle);

    viewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
    viewModel.getAnswer(1).observe(this, new Observer<String>() {
        @Override
        public void onChanged(@Nullable String answer) {
            viewModel.setAnswer(1, answer);
            tv_answer1.setText(answer);
        }
    });
}

Shared View Model:

public class SharedViewModel extends ViewModel {


public HashMap<Integer, MutableLiveData<String>> answers = new HashMap<Integer, MutableLiveData<String>>(){{
    put(1, new MutableLiveData<String>());
    put(2, new MutableLiveData<String>());
    put(3, new MutableLiveData<String>());
}};



public MutableLiveData<String> getAnswer(int questionId) {
    return answers.get(questionId);
}

public void setAnswer(int questionId, String answer) {
    if (answers.get(questionId) != null) {
        answers.get(questionId).setValue(answer);
    }
}
}

Solution

  • You just need a shared ViewModel which stores a list of LiveData for answers and maybe a list for questions.

    Here is an updated version of your SharedViewModel:

    public class ContestViewModel extends ViewModel {
    
    private HashMap<Int,LiveData<String>> answers = new HashMap<>();
    
    public LiveData<String> getAnswer(int questionId) {
        return answers.get(questionId);
    }
    public void setAnswer(int questionId, String answer) {
        // TODO make sure that answers.get is not null
        answers.get(questionId).setValue(answer);
      }
    }
    

    Now, in each fragment just call viewModel.getAnswer(id). So, each of them has a separated livedata for its question/answer.