因此,我正在嘗試爲我的一位是鐵桿運動愛好者的朋友製作一個應用程序,並且我被困在應用程序的一部分中。通過任何方法節省日期和時間
我試圖讓它自動保存每一次他的鍛鍊的日期和時間,所以他可以回頭看看他最後一次鍛鍊的時間以及他在這個月裏做了多少次這樣的事情。
但是,我不知道如何去做這件事。我對SharedPreferences的知識不足,我無法集成Google日曆。有沒有其他辦法可以發生這樣的事情?
如果沒有,有人可以幫我使用SharedPreferences部分嗎?
謝謝。
因此,我正在嘗試爲我的一位是鐵桿運動愛好者的朋友製作一個應用程序,並且我被困在應用程序的一部分中。通過任何方法節省日期和時間
我試圖讓它自動保存每一次他的鍛鍊的日期和時間,所以他可以回頭看看他最後一次鍛鍊的時間以及他在這個月裏做了多少次這樣的事情。
但是,我不知道如何去做這件事。我對SharedPreferences的知識不足,我無法集成Google日曆。有沒有其他辦法可以發生這樣的事情?
如果沒有,有人可以幫我使用SharedPreferences部分嗎?
謝謝。
在這裏,我爲你創造了一個設計。您可以根據您的要求進行必要的更改。
//Create a dbHelper class like this
public class DatabaseNew extends SQLiteOpenHelper {
private static final String TAG = "DataBase";
public DatabaseNew(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Module1";
private static final String USERTABLE = "USER_INFO";
private static final String KEY_ID = "id";
private static final String WORKOUT = "WORKOUT";
private static final String WORKOUT_TIME = "WORKOUT_TIME";
private static final String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS "
+ USERTABLE + "(" + KEY_ID + " INTEGER PRIMARY KEY," + WORKOUT + " TEXT,"
+ WORKOUT_TIME + " TEXT"+");";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + USERTABLE;
@Override
public void onCreate(SQLiteDatabase db) {
Log.e(TAG,"Inside onCreate: creating table");
db.execSQL(CREATE_TABLE_USER);
}
//to save workout name and time in db
public void saveData(String workout, String time){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put(WORKOUT,workout);
data.put(WORKOUT_TIME,time);
db.insert(USERTABLE,null,data);
db.close();
}
//to get workout name and time from db
public List<HashMap<String,String>> getData(){
List<HashMap<String,String>> userData = new ArrayList<>();
String selectQuery = "SELECT * FROM " + USERTABLE ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String,String> dataMap = new HashMap<>();
dataMap.put(WORKOUT,cursor.getString(cursor.getColumnIndex(WORKOUT)));
dataMap.put(WORKOUT_TIME,cursor.getString(cursor.getColumnIndex(WORKOUT_TIME)));
userData.add(dataMap);
} while (cursor.moveToNext());
}
cursor.close();
return userData;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG,"Inside onUpgrade");
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
//然後,你的活動中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
//Initializing the db
DatabaseNew db = new DatabaseNew(this);
//sending dummy data you can send actual workout and time like this
db.saveData("pull up","30 mins");
db.saveData("push up","40 mins");
db.saveData("Leg up","30 mins");
db.saveData("crunches","30 mins");
db.saveData("Bench Press","30 mins");
//to retrieve data from data base
List<HashMap<String,String>> data = db.getData();
for (HashMap<String,String> a : data){
Log.e("TestActivity"," "+"Workout : "+a.get("WORKOUT")
+" "+"Time : "+a.get("WORKOUT_TIME"));
}
}
希望它可以幫助!
謝謝一堆!這實際上有很大幫助。 –
然後請upvote它作爲答案 – sumit
如果你想存儲一個月的數據使用db,而不是在SharedPreference中維護數據。 – sumit
@sumit好的抱歉,這是我不清楚的原因。當我說月份時,這只是一個時間的例子。我真的希望它永久保存。 –
那麼你應該去數據庫,因爲SharedPreference用於存儲一小塊數據像一小會兒像維護一個會話。 – sumit