我正在创建一个应用程序,用户可以为朋友的生日设置提醒,但我坚持将日期类型存储在数据库中,并且还设置了通知,正如我在标题中所述,在指定之前日期。
我该怎么做?
这是我的代码,我只是在文本按钮上显示用户选择的日期,但我想存储该日期并在特定日期前2-3周设置通知。
这是我的代码:
@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
// set selected date in button
addBirthdayDate.setText(new StringBuilder().append(mMonth + 1)
.append("-").append(mDay).append("-").append(mYear)
.append("-"));
}
}
数据库代码(我在数据库中尝试了一些东西,但我不确定我在做什么)。所以我很感激这里的任何帮助:
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "_database";
private static final String BIRTHDAY_TABLE_NAME = "birthday_table";
public static final String BIRTHDAY_ID = "birthday_id";
public static final String BIRTHDAY_NAME = "birthday_name";
public static final String BIRTHDAY_LAST_NAME = "birthday_last_name";
public static final String BIRTHDAY_GENDER = "birthday_gender";
public static final String BIRTHDAY_AGE = "birthday_age";
public static final String BIRTHDAY_DATE = "birthday_date";
private static final String CREATE_TABLE = "CREATE TABLE " + BIRTHDAY_TABLE_NAME + " ( "
+ BIRTHDAY_ID + " INTEGER PRIMARY KEY,"
+ BIRTHDAY_NAME + " TEXT,"
+ BIRTHDAY_LAST_NAME + " TEXT,"
+ BIRTHDAY_GENDER + " TEXT,"
+ BIRTHDAY_DATE + " DATETIME,"
+ BIRTHDAY_AGE + " INTEGER );";
SQLiteDatabase database;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + BIRTHDAY_TABLE_NAME);
onCreate(db);
}
public String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
public void setBirthdayData(String birthdayName, String birthdayLastName, String birthdayGender
,int age) {
database = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(BIRTHDAY_NAME, birthdayName);
cv.put(BIRTHDAY_LAST_NAME, birthdayLastName);
cv.put(BIRTHDAY_GENDER, birthdayGender);
cv.put(BIRTHDAY_AGE, age);
cv.put(BIRTHDAY_DATE, getDateTime());
database.insert(BIRTHDAY_TABLE_NAME, null, cv);
}
答案 0 :(得分:0)
如果您仍然有这种担忧,我认为没有必要将其存储在数据库中。只需将其存储在变量中,随时设置警报管理器,如果您不再需要通知,则可以将其删除。
如果您仍想在数据库中存储日期时间,我建议将它们存储为Integer。这是我在我的应用程序中存储时间戳的示例:
private static final String CREATE_TABLE_NOTE = "create table " + Constants.NOTES_TABLE + "(" + Constants.COL_ID + " integer primary key autoincrement, " + Constants.COL_TITLE + " text not null, " + Constants.COL_CONTENT + " text, " + Constants.COL_TYPE + " text not null, " + Constants.COL_CATID + " integer not null, " + Constants.COL_CREATED_TIME + " integer not null, " + Constants.COL_MODIFIED_TIME + " integer not null " + ")";