How to pass data from one fragment to another

Asked

Viewed 684 times

1

Hello!

I received the following exercise:

• Fragment 1 must have two text boxes where the user can enter two numbers that will be added, and a button to trigger the addition

• Fragment 2 must have a Textview in the center of the screen to display the result of the addition.

I can make the first fragment:

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private EditText campo1, campo2;
private TextView campo3;
private double n1, n2, n3;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public frag1() {

}



public static frag1 newInstance(String param1, String param2) {
    frag1 fragment = new frag1();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    campo1 = (EditText) getActivity().findViewById(R.id.num1);
    campo2 = (EditText) getActivity().findViewById(R.id.num2);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

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

    return inflater.inflate(R.layout.fragment_frag1, container, false);
}


public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
public void somar() {
    n1 = Double.parseDouble(campo1.getText().toString());
    n2 = Double.parseDouble(campo2.getText().toString());
    n3 = n1 + n2;
}

Now, how do I send the value of the N3 variable to a Textview in another fragment?

2 answers

4

Here’s an example of how to send:

Adding to Fragment (this part you already do in your example):

  private static final String RESULTADO = "RESULTADO";
      public static SegundoFragment newInstance(int resultado) {
                SegundoFragment fragment = new SegundoFragment();
                Bundle args = new Bundle();
                args.putInt(RESULTADO , resultado);
                fragment.setArguments(args);
                return fragment;
            }

Now let’s take this amount in the Second Fragment:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_dois, container, false);
                TextView textView = (TextView) rootView.findViewById(R.id.seu_text);
//Exibimos o valor que veio com a TAG RESULTADO

                textView.setText(  getArguments().getInt(RESULTADO).toString() );
                return rootView;
            }

-1

Giving error in frag2 "error: cannot find Symbol variable RESULT"

Follows frag1 and frag2 code...

public class frag1 extends Fragment {

    private EditText campo1, campo2;
    private TextView campo3;
    private int n1, n2, n3,n4;
    private static final String RESULTADO = "RESULTADO";

    public frag1() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_frag1, container, false);
    }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
            campo1 = (EditText) getActivity().findViewById(R.id.num1);
            campo2 = (EditText) getActivity().findViewById(R.id.num2);
            campo3 = (TextView) getActivity().findViewById(R.id.textView);

        Button b = (Button)getActivity().findViewById(R.id.btnSomar);
        b.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n1 = Integer.parseInt(campo1.getText().toString());
                n2 = Integer.parseInt(campo2.getText().toString());
                n3 = n1 + n2;
                campo3.setText(String.valueOf(n3));
            }
        });}
    public static frag2 newInstance(int resultado) {
        frag2 fragment = new frag2();
        Bundle args = new Bundle();
        args.putInt(RESULTADO , resultado);
        fragment.setArguments(args);
        return fragment;
    }
}

FRAG2 where it is accusing the error, the word RESULT inside setText is in red.

    private TextView textView2;
View rootview;
public frag2() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_frag2, container, false);
    TextView textView = (TextView) rootView.findViewById(R.id.textView2);


    textView.setText(  getArguments().getInt(RESULTADO).toString() );
    return rootView;
}
  • Matheus, this post is an answer or a new question?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.