123 :
Edit
I modified some codes and added more descriptions based on the comment. :)
Edit 2
I found a way to perform the same function without using the while loop as follows:
if (lastDay != currentDay)
{
lastDay = currentDay;
//reset counter
daily_counter = 0;
}
if (lastDay == currentDay)
{
daily_counter++;
}
I am hoping to add 1 to count
if lastDay
is equal to currentDay
.
However, when lastDay == currentDay
, it does not add 1 after the first execution.
Here is my Java code:
//get current day
Calendar calendar = Calendar.getInstance();
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
SharedPreferences settings = getSharedPreferences("PREFS", 0);
int lastDay = settings.getInt("day",0);
//compare current day and last day
while (true) {
if (lastDay == currentDay) {
SharedPreferences.Editor editor = settings.edit();
//If a user collected an item, increase the daily counter
daily_counter++;
//commit to system
editor.putString("daily_counter", Integer.toString(daily_counter));
//commit
editor.commit();
//move to detailed description
Intent int1 = new Intent(bottom_nav_camera_yes_button.this, bottom_nav_search_details.class);
startActivity(int1);
break;
} else {
SharedPreferences.Editor editor = settings.edit();
editor.putInt("day", currentDay);
//reset counter
daily_counter = 0;
}
}
Just in case anyone needs, I am more familiar with C++, and it is working with my C++ codes as follows:
#include <iostream>
using namespace std;
int main()
{
int lastDay, currentDay, daily_counter = 0;
cout << "enter last day: " << endl;
cin >> lastDay;
cout << "enter current day: " << endl;
cin >> currentDay;
while (true)
{
if (lastDay == currentDay)
{
daily_counter++;
break;
}
else
{
lastDay = currentDay;
//reset counter
daily_counter = 0;
}
}
cout << "daily counter: " << daily_counter << endl;
return 0;
}
quealegriamasalegre :
try this just to see if the counter still isnt working
//get current day
Calendar calendar = Calendar.getInstance();
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
SharedPreferences settings = getSharedPreferences("PREFS", 0);
int lastDay = settings.getInt("day",0);
int daily_counter=Integer.valueOf(settings.getString("daily_counter","0"));
SharedPreferences.Editor editor = settings.edit();
//compare current day and last day
if (lastDay == currentDay) {
//If a user collected an item, increase the daily counter
daily_counter++;
//commit to system
editor.putString("daily_counter", Integer.toString(daily_counter));
//commit
editor.commit();
//move to detailed description
Intent int1 = new Intent(bottom_nav_camera_yes_button.this, bottom_nav_search_details.class);
startActivity(int1);
break;
} else {
editor.putInt("day", currentDay);
//commit
editor.commit();
//reset counter
daily_counter = 0;
}
after running it once the counter should be at 0 after running it twice it will be at 1 then 2 and so on. you would still have to add some code ensuring it only runs once a day
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=347487&siteId=1