使用SharedPreferences,值不会增加?

时间:2016-03-13 14:17:37

标签: android sharedpreferences

我正在尝试在Android Studio中执行一个增加其过去值的公式,但是不是递增最后一个值,而是用新值替换最后一个值。这是公式:

                    insCalories += (insValue * nutritional_Cal) /100;
                    insProtein += (insValue * nutritional_Pro) /100;
                    insFats += (insValue * nutritional_Fat) /100;
                    insCarbs += (insValue * nutritional_Car) /100;

(每次用户点击某个按钮时都会执行公式)

但公式是,至少在TextViews中显示,好像它是这样设置的:

                    insCalories = (insValue * nutritional_Cal) /100;
                    insProtein = (insValue * nutritional_Pro) /100;
                    insFats = (insValue * nutritional_Fat) /100;
                    insCarbs = (insValue * nutritional_Car) /100;

我还尝试将其更改为inCalories + = 5以查看公式本身是否有问题,但是显示insCalories,insProtein等的TextViews仍然显示数据,好像公式是insCalories = 5

所以我认为我可能使用SharedPreferences做错了,但我也没有发现任何错误。

每次用户点击按钮时,都会执行公式,并使用我的save()方法将新值存储在SharedPreferences中。这就是它的作用:

        public final void save() {

        SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong("Calories", Double.doubleToRawLongBits(insCalories));
        editor.putLong("Protein", Double.doubleToRawLongBits(insProtein));
        editor.putLong("Fats", Double.doubleToRawLongBits(insFats));
        editor.putLong("Carbs", Double.doubleToRawLongBits(insCarbs));

        editor.apply();

}

然后当用户导航到显示数据的活动时,我使用OnCreateView中的方法检索新值并将它们设置为在TextViews中显示:

double SPCalories;
double SPProtein;
double SPFats;
double SPCarbs;

String dc_text;
String dp_text;
String d_cartext;
String df_text;

TextView Calories_text;
TextView protein_text;
TextView carbs_text;
TextView fats_text;

Context CTX;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_daily_intake, container, false);

    Calories_text = (TextView) rootView.findViewById(R.id.Calories_text);

    protein_text = (TextView) rootView.findViewById(R.id.protein_text);

    carbs_text = (TextView) rootView.findViewById(R.id.carbs_text);

    fats_text = (TextView) rootView.findViewById(R.id.fats_text);

    CTX = getContext();

    SharedPreferences sharedPreferences = getActivity().getSharedPreferences("MyData", Context.MODE_PRIVATE);
    SPCalories = Double.longBitsToDouble(sharedPreferences.getLong("Calories", Double.doubleToLongBits(0)));
    SPProtein = Double.longBitsToDouble(sharedPreferences.getLong("Protein", Double.doubleToLongBits(0)));
    SPFats = Double.longBitsToDouble(sharedPreferences.getLong("Fats", Double.doubleToLongBits(0)));
    SPCarbs = Double.longBitsToDouble(sharedPreferences.getLong("Carbs", Double.doubleToLongBits(0)));

    DecimalFormat db = new DecimalFormat("#.#");
    db.setRoundingMode(RoundingMode.CEILING);

    dc_text = db.format(SPCalories);
    dp_text = db.format(SPProtein);
    d_cartext = db.format(SPCarbs);
    df_text = db.format(SPFats);


    Calories_text.setText(String.format(getString(R.string.Calories), dc_text));
    protein_text.setText(String.format(getString(R.string.Protein), dp_text));
    carbs_text.setText(String.format(getString(R.string.Carbs), d_cartext));
    fats_text.setText(String.format(getString(R.string.Fats), df_text));

如果您对我的错误有任何疑问,请告诉我,我们将不胜感激:)

维达尔

0 个答案:

没有答案