I do not reflect what put in the putString

0

Hi, first of all I'm starting with this android programming. The point is the following. I am trying to create an application based on my basic knowledge for the registration of a telephone number which, once registered, can send pre-defined messages. The point is as follows, I created an activity for the record and one for the "Panel" . Well, my problem is that when I register the number of my activity, when I want the values to pass to the textView of the panel, it is not shown to me.

Registration activity

SharedPreferences dregTele = getPreferences(context.MODE_PRIVATE);
SharedPreferences.Editor editor = dregTele.edit();
editor.putString("DNumero", nTelefono.getText().toString()); //REGISTRO NUMERO TELEFONICO
SharedPreferences dregName = getPreferences(context.MODE_PRIVATE);
SharedPreferences.Editor editor2 = dregName.edit();
editor2.putString("DName", nTransmisor.getText().toString()); //REGISTRO DEL NOMBRE DEL TRANSMISOR
editor.commit();
editor2.commit();
Intent i = new Intent(Registro.this, Seleccion.class);
Bundle bundle = new Bundle();
bundle.putString("DNumero","");
bundle.putString("DName","");
i.putExtras(bundle);
startActivity(i);

Use the Shared, because as I have seen, it is used for the storage of internal data of the telephone. Now, when I make the transition to the activity two that would be the panel it only shows me what is put in the key value of the bundle.putString

bundle.putString("DNumero","");
bundle.putString("DName","");

and it does not show what I put in the editText. The point is, that something must be entered through the editText. Store it through the SharpedPreferences and send it to the panel activity, and use them in that activity. Thank you!

    
asked by r00t 26.10.2017 в 15:44
source

1 answer

0

You do not need to create 2 Editor , with one enough:

// Obtienes los Preferences
SharedPreferences prefs = getPreferences(context.MODE_PRIVATE);

// Obtienes el Editor
SharedPreferences.Editor editor = prefs.edit();

// Usando el mismo Editor, guardas los 2 valores que necesitas
editor.putString("DNumero", nTelefono.getText().toString()); //REGISTRO NUMERO TELEFONICO
editor.putString("DName", nTransmisor.getText().toString()); //REGISTRO DEL NOMBRE DEL TRANSMISOR

// Confirmas los cambios en el Editor
editor.commit();

Intent i = new Intent(Registro.this, Seleccion.class);
Bundle bundle = new Bundle();
bundle.putString("DNumero", nTelefono.getText().toString()); // Aquí debes enviar lo que tienes en tu EditText (tambien en la linea siguiente)
bundle.putString("DName", nTransmisor.getText().toString());
i.putExtras(bundle);
startActivity(i); // DName

Now, in your activity Seleccion.java you will have a method like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xxx);

    // Aquí es en donde obtines lo que se capturó en tus EditText
    String dNumero = savedInstanceState.getBundle("DNumero");
    String dName = savedInstanceState.getBundle("DName");
}

Keep in mind, that in the onCreate() method you will be receiving the values you send in Bundle , not the values that you save in SharedPreferences . However, if you want to use the values that are in SharedPreferences , that would be your method onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ... mas código tuyo

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    String dNumero = sharedPreferences.getString("DNumero", null);
    String dName = sharedPreferences.getString("DName", null);
}
    
answered by 27.10.2017 в 16:58