Error in SharedPreferences

0

Is my program I want to save and read certain configuration data but for some reason they are not saved or can not read correctly, could someone tell me what my error is?

Code to save:

        int mConfigBaudRate = 115200;
        int mConfigDataBit = 8;
        int mConfigBitStop = 1;
        int mConfigParity = 0;
        String mFlagConexion = "";

    public void guardarConfigSerialPortShareref(){
      SharedPreferences myPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
      SharedPreferences.Editor editor = myPreferences.edit();
      editor.putInt("mconfigbaudratex", mConfigBaudRate);
      editor.putInt("mconfigdatabitx", mConfigDataBit);
      editor.putInt("mconfigbitstopx", mConfigBitStop);
      editor.putInt("mconfigparityx", mConfigParity);
      editor.putString("mFlagConexionx", "OK");
      editor.commit();
}

Code to read:

    public void leerConfigSerialPortShareref() {
      SharedPreferences myPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
      mConfigBaudRate = myPreferences.getInt("mconfigbaudratex", 0);
      mConfigDataBit = myPreferences.getInt("mconfigdatabitx", 0);
      mConfigBitStop = myPreferences.getInt("mconfigbitstopx", 0);
      mConfigParity = myPreferences.getInt("mconfigparityx", 0);
      mFlagConexion = myPreferences.getString("mFlagConexionx", "");
}
    
asked by W1ll 21.12.2018 в 00:11
source

2 answers

1

So I save in SharedPreferences and retrieve it like this:

preferences =getSharedPreferences("Record", MODE_PRIVATE);
                    String PrefAccumulated =preferences.getString("Acc","acc_defecto");
                    String PrefMissing = preferences.getString("miss","miss_defecto");
                    String PrefDone = preferences.getString("done","done_defecto");

                    String Accumulated = (String.format("%s",CumpViaje));
                    String Missing = (String.format("%s",CumpKmsG));
                    String Done = (String.format("%s",CumpKms));

                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("Acc", Accumulated);
                    editor.putString("miss", Missing);
                    editor.putString("done",Done);
                    editor.commit();

ASi I recover it:

preferences = this.getActivity().getSharedPreferences("Record", MODE_PRIVATE);
       String acumulado = preferences.getString("Acc",Acumulado);
       String faltantes = preferences.getString("miss",Faltantes);
       String realizados = preferences.getString("done",Realizados);
            tvKmsAccumulated.setText(acumulado);
            tvKmsMissing.setText(faltantes);
            tvKmsDone.setText(realizados);

and so I delete it:

preferencesRecord = getSharedPreferences("Record", Context.MODE_PRIVATE)
        val editorRecord = preferencesRecord.edit()
        editorRecord.clear()
        editorRecord.commit()

I hope it serves you

    
answered by 21.12.2018 / 00:26
source
0

Your code is correct, if you are not getting the values in the leerConfigSerialPortShareref() method the reason should be because you are not previously saving the values, I mean you do not call the guardarConfigSerialPortShareref() method, it ensures that this method is called previously.

I suggest you check:

Save SharedPreferences by assigning a key using getDefaultSharedPreferences ()

    
answered by 21.12.2018 в 00:26