android sharedpreferences中的变量增量

问题描述 投票:0回答:1

我正在开发一个应用程序,在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");} }
android sharedpreferences
1个回答
1
投票

修改这个

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

对此

////////////////////////////////////////////////////////////////
quardatudocerto = save.getInt ("valor", 0); 
quardatudocerto = quardatudocerto + num;
//////////////////////////////////////////////////////////////
© www.soinside.com 2019 - 2024. All rights reserved.