/** * updateUserData: update data when getting finish a training ( return from CyclingFinishDialog ) */ private void updateUserData(int time) { // get original data from shared preferences SharedPreferences sharedPreferences = mContext.getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE); String account = sharedPreferences.getString(MyDatabase.UserProfile.KEY_ACCOUNT, ""), name = sharedPreferences.getString(MyDatabase.UserProfile.KEY_NAME, ""), password = sharedPreferences.getString(MyDatabase.UserProfile.KEY_PASSWORD, ""), age = sharedPreferences.getString(MyDatabase.UserProfile.KEY_AGE, ""), lastDate = sharedPreferences.getString(MyDatabase.UserProfile.KEY_LAST_DATE, "0"); int gender = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_GENDER, 0), height = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_HEIGHT, 0), weight = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_WEIGHT, 0), level = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_LEVEL, 0), totalTimes = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_TOTAL_TIMES, 0), continueDays = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_CONTINUE_DAYS, 0), totalEnergy = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_TOTAL_ENERGY, 0), totalTime = sharedPreferences.getInt(MyDatabase.UserProfile.KEY_TOTAL_TIME, 0); // update data // TODO: update calorie data totalTimes++; continueDays = CommonFunction.calculateContinueDays( continueDays, lastDate, CommonFunction.getCurrentDate()); lastDate = CommonFunction.getCurrentDate(); totalTime += time; // set new data for shared preferences SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(MyDatabase.UserProfile.KEY_TOTAL_TIME, totalTime); editor.putInt(MyDatabase.UserProfile.KEY_CONTINUE_DAYS, continueDays); editor.putInt(MyDatabase.UserProfile.KEY_TOTAL_ENERGY, totalEnergy); editor.putInt(MyDatabase.UserProfile.KEY_TOTAL_TIMES, totalTimes); editor.putString(MyDatabase.UserProfile.KEY_LAST_DATE, lastDate); editor.apply(); // set new data for database MyDatabase mDatabase = MyDatabase.getInstance(mContext); try { mDatabase.open(); } catch (SQLException e) { e.printStackTrace(); } Cursor c = mDatabase.checkUserBasicPassword(account, password); if (c.moveToFirst()) { /*(String account, String username, String password, String age, int gender, int height, int weight, int level, int total_time, int total_times, String last_date, int continue_days, int total_energy){*/ mDatabase.updateUserBasic( account, name, password, age, gender, height, weight, level, totalTime, totalTimes, lastDate, continueDays, totalEnergy); } mDatabase.close(); }