/** initListView: initialize the ListView */ private void initListView() { courses = new ArrayList<>(); generateFakeData(); ListView listView = (ListView) mView.findViewById(R.id.content).findViewById(R.id.list_view); TitleIntroTimeAdapter adapter = new TitleIntroTimeAdapter(mContext, courses); listView.setAdapter(adapter); listView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(mContext, CyclingActivity.class); Bundle bundle = new Bundle(); bundle.putString(GlobalType.COURSE_NAME, titles[position]); bundle.putInt(GlobalType.TIME_PERIOD1, times[position].time1); bundle.putInt(GlobalType.TIME_PERIOD2, times[position].time2); bundle.putInt(GlobalType.TIME_PERIOD3, times[position].time3); bundle.putInt(GlobalType.TIME_PERIOD4, times[position].time4); intent.putExtras(bundle); startActivityForResult(intent, 0); } }); listView.setDivider(null); CommonFunction.setListViewHeightBasedOnChildren(listView); }
/** * 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(); }