sharedPreferences帮助。如何保存int?

时间:2013-01-23 06:21:45

标签: java android sharedpreferences android-preferences

我不能为我的生活似乎弄清楚如何保存int。我想要做的就是将leftCounter保存到文件中,并在每次重新启动代码时检索它。

public class SalesTrack extends Activity {

   byte leftCounter = 0;
   byte centerCounter = 0;
   byte rightCounter = 0;
   int total;
   int x, y, z;

   public static final String PREFS = "data";

   TextView counterAAL, counterUPG, counterNL, counterTotal;

   private final String LEFTY = "6";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_sales_track);

       counterAAL = (TextView) findViewById(R.id.left_number);
       counterUPG = (TextView) findViewById(R.id.center_number);
       counterNL = (TextView) findViewById(R.id.right_number);
       counterTotal = (TextView) findViewById(R.id.total);

       SharedPreferences sharedPref = this.getSharedPreferences(LEFTY, 0);
       sharedPref.getInt("data", leftCounter);
       counterAAL.setText(PREFS);
   }

   public void LeftInc(View v) {
       leftCounter++;
       counterAAL.setText("" + leftCounter);
       Refresh();
   }

   public void LeftDec(View v) {
       leftCounter--;
       counterAAL.setText("" + leftCounter);
       Refresh();
   }

   public void CenterInc(View v) {
       centerCounter++;
       counterUPG.setText("" + centerCounter);
       Refresh();
   }

   public void CenterDec(View v) {
       centerCounter--;
       counterUPG.setText("" + centerCounter);
       Refresh();
   }

   public void RightInc(View v) {
       rightCounter++;
       counterNL.setText("" + rightCounter);
       Refresh();
   }

   public void RightDec(View v) {
       rightCounter--;
       counterNL.setText("" + rightCounter);
       Refresh();
   }

   // Refresh Total after every click.
   public void Refresh() {
       x = leftCounter * 10;
       y = centerCounter * 20;
       z = rightCounter * 35;
       total = x + y + z;
       counterTotal.setText("" + total);

       SharedPreferences sharedPref = getSharedPreferences(PREFS, 0);
       SharedPreferences.Editor editor = sharedPref.edit();
       editor.putInt("data", leftCounter);
       editor.commit();
   }
}

6 个答案:

答案 0 :(得分:2)

您的代码中有错误。

// SharedPreferences sharedPref = getSharedPreferences(PREFS, 0); (This line is wrong)
      SharedPreferences sharedPref = getSharedPreferences(LEFTY, 0);
      SharedPreferences.Editor editor = sharedPref.edit();
      editor.putInt("data", leftCounter);
      editor.commit();

答案 1 :(得分:0)

所以你从文件中获取数据,但它被检测为String

您可以使用解析功能将String转换为Integer

int counter = Integer.parseInt(stringvalue);

答案 2 :(得分:0)

您可以使用两个不同的名称LEFTYPREFS获取偏好设置。鉴于名称,我认为您要更改onCreate

中的以下行
SharedPreferences sharedPref = this.getSharedPreferences(LEFTY, 0);

SharedPreferences sharedPref = this.getSharedPreferences(PREFS, 0);

答案 3 :(得分:0)

SharedPreferences sharedPref = getSharedPreferences(LEFTY, 0);
SharedPreferences.Editor editor = sharedPref .edit();               
editor.putInt("data", leftCounter);
editor.commit();

答案 4 :(得分:0)

像这样改变

SharedPreferences sharedPref = = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("data", leftCounter);
editor.commit();

答案 5 :(得分:0)

您正在保存名为data的{​​{1}}内部偏好设置并通过偏好设置名称PREFS

尝试在LEFTY下将您的偏好设置名称从LEFTY更改为PREFS

onCreate()