Load different texts in the same TextView (SharedPreferences)

2

I have two EditText and what I write I keep it in SharedPreferences and I charge it in TextView but I can only load the text of a EditText and not both texts. What I want is to load the two texts in the same TextView .

For example if I write in the first EditText Hello, and in the second Qué tal in my TextView it loads like this: Hello, What such .

My code is this:

public class MainActivity extends AppCompatActivity {

    EditText uno, dos;
    TextView receptor;

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

        uno = (EditText) findViewById(R.id.uno);
        dos = (EditText) findViewById(R.id.dos);
        receptor = (TextView) findViewById(R.id.receptor);

        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);

        // guardo los textos en sharedpreferences

        uno.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                prefs.edit().putString("uno", s.toString()).commit();
            }
        });

        dos.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                prefs.edit().putString("dos", s.toString()).commit();
            }
        });


        // cargo los textos guardados en SharedPreferences

        receptor.setText(prefs.getString("uno", "" + "dos"));

    }

}

As you can see in my code I have tried with receptor.setText(prefs.getString("uno", "" + "dos")); but in my TextView I always receive what I wrote in uno .

Is it possible to do what I ask? Thanks!

    
asked by UserNameYo 19.05.2017 в 22:58
source

1 answer

3

Remember the method getString () of preferences:

  

getString ( key, DefValue)

     
  • Key : String with the name of the preference to be recovered.
  •   
  • DefValue : String with value to return if this preference does not exist (default value).
  •   

What you are doing is that you give the value of the preference with name "one" and if it does not exist, you write the text "two":

  receptor.setText(prefs.getString("uno", "" + "dos"));

To obtain the value of both preferences it should be in this way, as an example defining in both a default value of "", this you decide:

String valor1 = prefs.getString("uno", ""); 
String valor2 = prefs.getString("dos", ""); 

therefore to add the value of both preferences to your TextView , this would be the correct way:

receptor.setText(prefs.getString("uno", "") + prefs.getString("dos", ""));
    
answered by 19.05.2017 / 23:18
source