Domanda

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);



}

}

È stato utile?

Soluzione 2

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();

Altri suggerimenti

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.put("key", value);
editor.commit();

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top