android sharedpreferences中的变量增量

时间:2017-12-29 05:05:21

标签: android sharedpreferences

我正在开发一个应用程序,在activity1中生成变量int,我可以将这些变量通过String从activity 1变为activity2,再次转换为int数据并使用SharedPreferences保存。问题,我不能用现有的值增加变量quardatudocerto并保存它。我尝试了一切,没有任何东西可以解决它可能是什么?

 BarChart chart;
 ArrayList <BarEntry> BARENTRY;
 ArrayList <String> BarEntryLabels;
 BarDataSet Bardataset;
 BarData BARDATA;
 int num;
 int num1;
 int num2;
 int quardatudocerto = 0;
 private int t = 0;
 private SharedPreferences save;
 private SharedPreferences.Editor editor;
@Override
 protected void onCreate (Bundle savedInstanceState) {
 super.onCreate (savedInstanceState);
 setContentView (R.layout. general performance);
 String str = DesempenhofinalActivity.apppassacerta.getString ("keycerta");
 String str1 = DesempenhofinalActivity.apppassaerrada.getString ("keyerrada");
 String str2 = DesempenhofinalActivity.apppassatot.getString ("keytotal");
 save = getSharedPreferences ("save", Context.MODE_PRIVATE);
 t = save.getInt ("value", 0); // retrieve the value stored in the "value" key and default 0
 if (str! = null) {
 try {
 num = Integer.parseInt (str);
 } catch (NumberFormatException e) {}
    }
  if (str1! = null) {
 try {
 num1 = Integer.parseInt (str1);
 } catch (NumberFormatException e) {}
 }
 if (str2! = null) {
 try {
 num2 = Integer.parseInt (str2);
 } catch (NumberFormatException e) {}
 } 
chart = (BarChart) findViewById (R.id.chart1);
BARENTRY = new ArrayList <> ();
BarEntryLabels = new ArrayList <String> ();
AddValuesToBARENTRY ();
AddValuesToBarEntryLabels ();
Bardataset = new BarDataSet (BARENTRY, "Wrong / Right / Total");
BARDATA = new BarData (BarEntryLabels, Bardataset);
Bardataset.setColors (ColorTemplate.COLORFUL_COLORS);
chart.setData (BARDATA);
chart.animateY (3000);
////////////////////////////////////////////////////////////////
quardatudocerto = quardatudocerto + num;
//////////////////////////////////////////////////////////////
}
@Override
protected void onStop () {
super.onStop ();
editor = save.edit ();
///////////////////////////////////////////////////////////////////
editor.putInt("valor", quardatudocerto);//seta o par de chave("valor") e valor(t)
 /////////////////////////////////////////////
 // the key pair ("value") and value (t)
editor.commit (); // records the preference}
public void AddValuesToBARENTRY () {
BARENTRY.add (new BarEntry (t, 0));
}
public void AddValuesToBarEntryLabels () {
BarEntryLabels.add ("Wrong");} }

1 个答案:

答案 0 :(得分:1)

修改此

////////////////////////////////////////////////////////////////
quardatudocerto = quardatudocerto + num;
//////////////////////////////////////////////////////////////

到这个

////////////////////////////////////////////////////////////////
quardatudocerto = save.getInt ("valor", 0); 
quardatudocerto = quardatudocerto + num;
//////////////////////////////////////////////////////////////