How do I update an editText of a fragment from another fragment in an activity Tabbed?

1

Good morning, I have a tabbed activity with three fragment, one of them is called frPrincipal and another one is called frConfiguracion. In frConfiguracion I have an editText where I enter an ip and a button with which I save using SharedPreferences. But I am not able that when the data is saved it is reflected in another editText of the frPrincipal. I have to close the application and reopen it to be able to see the data reflected.

This is what I have in the frConfiguracion button

public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    //debemos declarar esto para poder instanciar los objetos en el fragment
    View rootView = inflater.inflate(R.layout.fragment_fr_configuracion,container,false);

    if(rootView != null){
        final EditText etConfigDirIP = (EditText) rootView.findViewById(R.id.etConfigDirIP);
        Button btnConfigGuardar = (Button) rootView.findViewById(R.id.btnConfigGuardar);

        //Traigo la IP anteriormente guardada
        SharedPreferences sharpref  = getActivity().getPreferences(Context.MODE_PRIVATE);
        String valor = sharpref.getString("Dir_IP", "NO hay ip");
        etConfigDirIP.setText(valor.toString());

        btnConfigGuardar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Utilizo el shared preferences para poder guardar los datos

                SharedPreferences sharpref = getActivity().getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharpref.edit();
                editor.putString("Dir_IP",etConfigDirIP.getText().toString());
                editor.commit();
                Toast.makeText(getActivity(), "IP Guardada", Toast.LENGTH_LONG).show();


                //Pendiente que al guardar actualice la IP en el fragment principal

            }
        });
    }
    return  rootView;
}

And this is what I have in the frPrincipal

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


    View rootView = inflater.inflate(R.layout.fragment_fr_principal,container,false);

    if(rootView != null){
        //Instanciar
        final EditText etPpalDirIP = (EditText) rootView.findViewById(R.id.etPpalDirIP);
        final EditText etPpalDirCasa = (EditText) rootView.findViewById(R.id.etPpalDirCasa);
        final EditText etPpalComando = (EditText) rootView.findViewById(R.id.etPpalComando);
        final EditText etPpalMandar = (EditText) rootView.findViewById(R.id.etPpalMandar);
        final EditText etPpalRespuesta = (EditText) rootView.findViewById(R.id.etPpalResultado);

        Button btnMandar = (Button) rootView.findViewById(R.id.btnPpalMandar);


        //Traigo la IP guardada en configuracion, mirar si se puede traer desde el edit text de configuracion
        SharedPreferences sharpref  = getActivity().getPreferences(Context.MODE_PRIVATE);
        String valor = sharpref.getString("Dir_IP", "NO hay ip");
        etPpalDirIP.setText(valor.toString());

        //Metodo oncliclistener del boton
        btnMandar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mensaje;
                mensaje = etPpalDirIP.getText().toString() + "?"; //el interrogante es para separar el comando completo
                mensaje = mensaje + etPpalDirCasa.getText().toString() + etPpalComando.getText().toString();
                etPpalMandar.setText(mensaje);

                //llamo el subprograma que manda al servidor
                new MandarDatos().execute(mensaje);

                //llevar el valor de la respuesta al edittext

            }
        });

    }
    return rootView;
}  
    
asked by Carlos Andres Montoya 08.02.2017 в 14:13
source

1 answer

0

I'm sure there's a quicker and easier way but I do not know why I always go on the harder side: p.

What I can think of is that the configure button you can pass the data of your fragment x to your fragment y and then after you save in sharedPreferences :

FragmentX fragmentX = new FragmentX();
Bundle datos = new Bundle();
datos.putString("IP", "192.168.0.1");// Aquí lo he puesto directamente el string
fragmentX .setArguments(datos);

In your second fragmentY (tab) you receive the values, you set it to your Edittext and later you save the value in SharedPreferences , in this case I put it inside a function. Within Oncreateview you make a comparison to check if there is a value already saved:

sharpref  = getActivity().getPreferences(Context.MODE_PRIVATE);
String value = sharpref.getString("Dir_IP",null);
if (value == null) {
    guardarEnPreferencias(); // Si está vacío que guarde
} else {
 //En caso contrario que cargue los datos guardado
  SharedPreferences sharpref  = getActivity().getPreferences(Context.MODE_PRIVATE);
        String valor = sharpref.getString("Dir_IP", "NO hay ip");
        etConfigDirIP.setText(valor.toString());

}

 //Ojo, este método va fuera del OnCreateView

     public void guardarEnPreferencias(){
      //Recibes el valor y lo seteas a tu edittext
      String valorRecibido = this.getArguments().getString("IP");
      etConfigDirIP.setText(valorRecibido.toString());

      SharedPreferences sharpref = getActivity().getPreferences(Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = sharpref.edit();
      editor.putString("Dir_IP",etConfigDirIP.getText().toString());
      editor.commit();
      Toast.makeText(getActivity(), "IP Guardada", Toast.LENGTH_LONG).show();

}
    
answered by 08.02.2017 в 16:42