私は目覚まし時計アプリを作りたいと思います。実際には、私はすべてのアラームをデータベースに入れたいと思いますが、すでにこれを行っていますが、(LogCatを介して)アラームをチェックしたいとき、私は別のアラームを取得します。お願い助けて! mySQLiteHelper.class:なぜデータを保存する前後で結果が違うのですか?
public class mySQLiteHelper extends SQLiteOpenHelper {
// All Static variables
// Database Version
public static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "alarms.db";
// Contacts table name
private static final String TABLE_ALARMS = "alarms";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_ALARM = "alarm";
private static final String KEY_NAME = "name";
public mySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables"create table alarms ("
//+ "id integer primary key,"
// + "alarm text,"
// + "name text," // added a ','
// + "alarmname text" + ");"
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ALARMS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ALARM + " TEXT, "
+ KEY_NAME + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALARMS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations"," + alarm.getName() +,name
*/
// Adding new contact
void addAlarm(Alarm alarm) {
SQLiteDatabase db = this.getWritableDatabase();
String sql =
"INSERT or replace " +
"INTO alarms (alarm,"+KEY_NAME+") " +
"VALUES("+ alarm.getAlarm() + ",'" + alarm.getName() + "')";
//String sql =
// "INSERT or replace INTO alarms (alarm,alarmname) VALUES("+ alarm.getAlarm() + ",'" + alarm.getName() + "')" ;
db.execSQL(sql);
db.close(); // Closing database connection
}
// Getting single contact
String getAlarm(String alarmname) {
String query = "Select * FROM " + TABLE_ALARMS + " WHERE " + KEY_NAME + " = \"" + alarmname + "\"";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.getCount()>0)
cursor.moveToFirst();
String alarm = cursor.getString(cursor.getColumnIndex(KEY_ALARM));
cursor.close();
// return contact
return alarm;
}
// Deleting single contact
public void deleteAlarm(Alarm alarm) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ALARMS, KEY_ID + " = ?",
new String[] { String.valueOf(alarm.getID()) });
db.close();
}
// Getting contacts Count
public int getAlarmsCount() {
String countQuery = "SELECT * FROM " + TABLE_ALARMS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int i = cursor.getCount();
cursor.close();
// return count
return i;
}
}
Alarm.class:
public class Alarm {
//private variables
int _id;
String _name;
String _alarm;
// Empty constructor
public Alarm(String alarm,String alarmname){
this._alarm = alarm;
this._name = alarmname;
}
// constructor
public Alarm(int id, String name, String alarm){
this._id = id;
this._name = name;
this._alarm = alarm;
}
// constructor
public Alarm(String alarm){
this._alarm = alarm;
}
// getting ID
public int getID(){
return this._id;
}
// getting name
public String getName(){
return this._name;
}
// getting phone number
public String getAlarm(){
return this._alarm;
}
}
これは私が集合時間、addAlarmを呼び出し、初めて私のアラームを取得
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.MINUTE, timePicker1.getCurrentMinute());
calendar1.set(Calendar.HOUR_OF_DAY,timePicker1.getCurrentHour());
String alarm1 = Long.toString(calendar.getTimeInMillis());
String alarm2 = Long.toString(calendar1.getTimeInMillis());
Log.i("Alarm1.1",alarm1);
Log.i("Alarm2.1",alarm2);
mySQLiteHelper mySQLiteHelper = new mySQLiteHelper(MainActivity2.this);
Log.i("Point_version",Integer.toString(mySQLiteHelper.DATABASE_VERSION));
mySQLiteHelper.addAlarm(new Alarm(alarm1,"alarm1"));
mySQLiteHelper.addAlarm(new Alarm(alarm2,"alarm2"));
そして、どのようにありますしばらくすると私は警報を発する
String alarm1 = mySQLiteHelper.getAlarm("alarm1");
String alarm2 = mySQLiteHelper.getAlarm("alarm2");
Log.i("Alarm1.2",alarm1);
Log.i("Alarm2.2",alarm2);
私はこの結果得、これを試してみましたと思います:
Alarm1.1﹕ 1459098157713
Alarm2.1﹕ 1459098230836
Alarm1.2﹕ 1459095666194
Alarm2.2﹕ 1459095664808
時間と分を設定するためにcalendarとcalendar1コードを追加できますか? – GiapLee
@giaplee見て、私は私の質問を編集しました –