-4

I shared the preference value A after I run a calculation with B you can write directly the result of AB in the shared preference by replacing A with the new value

public class Stats extends Activity {
private final static String MY_PREFERENCES = "MyPref";
private final static String GIORNISPETT = "giornispett";    
public void indietro (View view){
}
@Override
public void onCreate(Bundle savedInstanceState) {
    SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
    int giornispett = Integer.parseInt(prefs.getString(GIORNISPETT, "24"));

    super.onCreate(savedInstanceState);
    setContentView(R.layout.stats);

    Intent intent=getIntent();
    String trn=getPackageName();
    int Giorni=intent.getIntExtra(trn+".Intgiorni", 0);
    int Giorniresidui=giornispett-Giorni;


    TextView tvgiorni=(TextView)findViewById(R.id.txtgiorni);        
    tvgiorni.append(+Giorni+"");


    TextView tvGiorni_rest=(TextView)findViewById(R.id.txtGiorni_rest);        
    tvGiorni_rest.setText(""+Giorniresidue);



}

}

  • 1
    There are multiple questions asking this already and a simple Google would have returned many basic examples and tutorials. Please do some research before posting new questions. – indivisible Apr 28 '14 at 14:22

3 Answers3

1
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.put("key", value);
editor.commit();
Boban S.
  • 1,662
  • 13
  • 16
0

Adding values to shared preferences works in key - value mode. If you use the same key twice the first value will be overwritten.

Dawid C
  • 413
  • 3
  • 10
0

The most easiest way it is to use SharedPreferences.Editor API with set of put API:

//update application settings based on check box state
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activityContext);
Editor editor = sharedPrefs.edit();
editor.putBoolean("optionName", !checkBox.isChecked());
editor.commit();
N0dGrand87
  • 727
  • 1
  • 9
  • 16
  • I tried but I can not figure out how to put – user3564888 Apr 28 '14 at 14:44
  • To put some value into application sharedPreferences you have to use Editor, and to activate editor just use edit() API. And than store your changes (for ex. refer to http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putBoolean(java.lang.String, boolean) ) with Editor.commit() API. – N0dGrand87 Apr 28 '14 at 15:10