-3

I'm developing an application which has two fragments. I need to get the text from EditText in one fragment into a TextView in the other.

I have been tried some option but the app is still crashing.

The first Fragment is Tab1Setup.

The second Fragment is Tab2Auto.

I tried:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //TODO slide to position
        View rootView = inflater.inflate(R.layout.tab1setup, container, false);


        slide = (TextView) rootView.findViewById(R.id.slide);
        textview = (TextView) rootView.findViewById(R.id.textView);

        MatchNumber = (EditText) rootView.findViewById(R.id.MatchNumber);
        ScouterName = (EditText) rootView.findViewById(R.id.ScouterName);
        TeamNumber1  = (EditText) rootView.findViewById(R.id.TeamNumber);
        TeamNumber2 = (EditText) rootView.findViewById(R.id.TeamNumber2);
        TeamNumber3 = (EditText) rootView.findViewById(R.id.TeamNumber3);

        Tab2Auto t2a = new Tab2Auto;

        t2a.textview.setText(TeamNumber1.getText.toString);

hope you can help.

Yazan
  • 6,074
  • 1
  • 19
  • 33
I.Segev
  • 1
  • 1
  • 1
    always post the error – John Joe Aug 06 '17 at 16:25
  • `Tab2Auto t2a = new Tab2Auto;` <-- This isn't valid java. In adddition, you don't want to create a new `Tab2Auto` fragment, but write to an existing one. See [Communicating with other Fragments](https://developer.android.com/training/basics/fragments/communicating.html) in the android docs. – PPartisan Aug 06 '17 at 16:28
  • 1
    Use Eventbus library for passing data between two fragments – Lakshay Jain Aug 06 '17 at 16:54
  • john joe the error is "App name has been stopped" – I.Segev Aug 06 '17 at 18:18

2 Answers2

2

accessing another fragment is not a good practice. try adding an interface in your Tab1Setup and implement it in your parent activity. then add a listener to your EditText and call a method of your interface. then in your parent activity, pass the value from Tab1Setup fragment to your Tab2Auto. for more information about communicating between fragment and activity read here.

example :

in your activity :

public class YourParentActivity extends AppCompatActivity 
     implements Tab1Setup.OnInteractionListener {
          // activity code 
          ...
          // when adding second fragment 
          mSecondFragment = Tab2Auto.newInstance();
          getFragmentManager()
              .beginTransaction()
              .add(R.id.container_id, mSecondFragment)
              .commit();
          ...


          // this is first fragment interface method
          @Override
          public void onEditTextValueChanged(String value) {
               mSecondFragment.updateValue(value);
          }
}

and in your fragments:

public class Tab1Setup extends Fragment { 
     private OnInteractionListener mListener;
     // fragment code

    ...
    // inside your listener for edit text value
    mListener.onEditTextValueChanged(value);
    ...



     public interface OnInteractionListener {
          void onEditTextValueChanged(String newValue);
     }
}

public class Tab2Auto extends Fragment {
    // fragment code
    public void updateValue(String value) {
        mTextView.setText(value);
    }
}
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
1

Send data from fragment to activity by intent, receive the data in activity. Again start an intent to send data from one activity to another activity. Then send this data in a bundle from activity to fragment. Get the data in oncreateview of fragment.

Code snippet- How to pass values between Fragments

soham97
  • 11
  • 2
  • You should explain the most important contents of your linked document in your answer. In this special case you should also add the code snippets in here. – Markus Aug 06 '17 at 17:10
  • Sure.It was my first answer hope you'll consider :) – soham97 Aug 06 '17 at 17:14
  • I just wanted to help you improve your answer. :) Just edit it and it will get better. :) – Markus Aug 06 '17 at 17:28