0

I insert the values ​​in sharedpreferences, now I do not know how to display these values ​​in a TextView.

SharedPreferences prefs = getSharedPreferences(SharedPrefName, 0);
        SharedPreferences.Editor prefsEditor = prefs.edit();

        float importo = prefs.getFloat( "Importo", 0 );
        float valore = Float.parseFloat( mImporto.getText().toString() );
        prefsEditor.putFloat( "Importo", importo + valore );
        prefsEditor.commit();

5 Answers5

1

First thing lets try to save something in SharedPreferences, I'm using a method because is easy to understand this way, and it looks cool :P

TO ADD in class:

private SharedPreferences settings;
private static final String PREFS_NAME = "app_name";

private TextView usernameTextView;
private TextView passwordTextView;


init textviews...

usernameTextView = (TextView)...
passwordTextView= (TextView)...

in constructor add this:

settings = context.getSharedPreferences(
            PREFS_NAME, 0);

--Save username and password

    public static final String USERNAME = "username";
    public static final String PASSWORD= "password";

    private void savePreferences(String sharedUsername, String sharedPassword) {

    SharedPreferences.Editor SharedEditor = settings.edit();
    SharedEditor.putString(USERNAME,
            sharedUsername);
    SharedEditor.putString(PASSWORD,
            sharedPassword);
    SharedEditor.commit();

}

--Load and display username and password in a textview

    private void loadSharedPreferences() { 
    if (settings != null) {
        String loadUsername= settings.getString(
                USERNAME , null);
        if (loadUsername != null && !loadUsername.isEmpty()) {
            usernameTextView.setText(loadUsername);
        }
        String loadPassword= settings.getString(
                PASSWORD, null);
        if (loadPassword!= null && !loadPassword.isEmpty()) {
            passwordTextView.setText(loadPassword);
        }
    }
}

Hope you use it. Cheers

MSA
  • 2,502
  • 2
  • 22
  • 35
0

just find your textview in your layout:

  TextView tv = (TextView)view.findViewById(R.id.yourtextview);
  tv.setText("your value");
Tobiel
  • 1,543
  • 12
  • 19
0

It depends where you want to set the value. If you just want to set the value after saving it in SharedPreference, then you can simple do:

TextView YourTextView = (TextView) findViewById(R.id.tv);
YourTextView.setText(String.valueOf(importo + valore) );

If you want to set it in some other class, then you can get the value from SharedPreferencce and set in in TextView. See How to use SharedPreferences in Android to store, fetch and edit values for how to get the values. Then after you extract the value, you can set it as I showed in the example above.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

Assuming you mean you need to retrieve these values in a different Activity, you retrieve the values with something like

SharedPreferences values = getSharedPreferences(SharedPrefName, 0);
float  valore = values .getFloat("Importo", 0);

Then get the String value and set your TextView or just do

textView.setText("" + valore);

The docs have a good example

SharedPreferences full docs

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

First getValue from Sharedprefrences :

float value = prefs.getFloat( "Importo", 0 );

set float value in your textview:

textview.setText(String.valueOf( value ));

Raj Singh
  • 1
  • 1