1

I already implemented a code from another tutorial but I don't know how to change the TextView with this code. I know that I have to use the setAnswer of the ViewModel but I don't know where to use it. I am relatively new to programming so your help is much appreciated! After the first question there are 2 other question each with a EditText that's input should also be displayed in the next fragment of the question.

Here is my ViewModel:

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

Here is my Fragment with the EditText:

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

    mEditTextQuestion1.addTextChangedListener(new NumberTextWatcher(mEditTextQuestion1));

    m_bar = view.findViewById(R.id.progress_bar_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) {
            tv_answer1.setText(answer);
        }
    });
}

And here is my second Fragment where the input of the previous EditText should be displayed in a TextView:

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) {
            tv_answer1.setText(answer);
        }
    });
}
}
Daniel Spatz
  • 77
  • 1
  • 14

1 Answers1

0
  1. Create a global variable(String) to store the text of your EdiText
  2. When you open the Fragment with your EditText you can use a Button (onClickListener) or onDetached to set your global variable.

    ((MyApplication) this.getApplication()).setSomeVariable(edittext.getText().toString());

  3. When you go back to your Fragment with the TextView, you add this to void onStart()

    textview.setText(((MyApplication) this.getApplication()).getSomeVariable());

Jakob
  • 1,858
  • 2
  • 15
  • 26
  • But why do I have then the setAnswer method in the ViewModel? – Daniel Spatz Sep 23 '19 at 17:32
  • @DanielSpatz I don't know. You don't need a ViewModel for this. You can set the text from your Fragments. – Jakob Sep 23 '19 at 17:35
  • This code is a complete new one but I want to know how to use the existing code to achieve the result. I mean I already call the textview.setText method – Daniel Spatz Sep 23 '19 at 17:36
  • The thing is that after the first question there are 3 other questions with EditText so I needed the ViewModel – Daniel Spatz Sep 23 '19 at 17:37
  • @DanielSpatz You can keep your ViewModel. But to change the TextView, it's the best way to use a global variable. It's also possible to access the String from your ViewModel. – Jakob Sep 23 '19 at 17:38
  • Ok since I am new to programming I honestly don't know exactly how to use the existing code to achieve this but thank you for your suggestions! – Daniel Spatz Sep 23 '19 at 17:40
  • Ok implemented the code but now the TextView shows nothing – Daniel Spatz Sep 23 '19 at 18:08