Hello
I received the following exercise:
• Fragment 1 should have two text boxes where the user can enter two numbers to 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 variable n3 to a TextView in another fragment?