嗨,我是初学的Android程序员。我试图在SQLiteDatabase中添加TimePicker小时和分钟。我的代码工作正常,但是当我查看数据库表时,我看到的唯一的事情是列小时和列分钟。下面是我的代码:我的主类,DatabaseHandler类和Constructor类。
我的MainActivity
课程:
public class MainActivity extends Activity {
static final int TIME_DIALOG_ID = 999;
private TextView tvDisplayTime;
private TimePicker timePicker1;
private Button btnChangeTime;
private int hour;
private int minute;
DataBaseHandler db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DataBaseHandler(this);
setCurrentTimeOnView();
addListenerOnButton();
}
public void setCurrentTimeOnView() {
tvDisplayTime = (TextView) findViewById(R.id.tvTime);
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
}
public void addListenerOnButton() {
btnChangeTime = (Button) findViewById(R.id.btnChangeTime);
btnChangeTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timePickerListener, hour, minute, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
final Calendar c = Calendar.getInstance();
hour = 5;
minute = 56;
String HourPrompt = String.valueOf(hour);
String MinutePrompt = String.valueOf(minute);
db.addTheTime(new TimeConstructor(HourPrompt, MinutePrompt));
Toast.makeText(getApplicationContext(), "Time Saved", Toast.LENGTH_LONG).show();
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
我的SQLiteOpenHelper
:
public class DataBaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "SampleDatabase";
private static final String TABLE_TIME = "time";
public static final String HOUR = "hour";
public static final String MINUTE = "minute";
public static final String CREATE_TIME = "create table TABLE_TIME (hour text not null, minute text not null);";
SQLiteDatabase db;
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TIME);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TIME);
onCreate(db);
}
public void addTheTime(TimeConstructor prompt) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues theTime = new ContentValues();
theTime.put(HOUR, prompt.hour);
theTime.put(MINUTE, prompt.minute);
db.insert(TABLE_TIME, null, theTime);
db.close();
}
public Cursor returndata() {
SQLiteDatabase db = this.getReadableDatabase();
return db.query(TABLE_TIME, new String[]{HOUR, MINUTE}, null, null, null, null, null);
}
}
我的TimeConstructor
课程:
public class TimeConstructor {
String hour;
String minute;
public TimeConstructor(String newHour, String newMinute) {
this.hour = newHour;
this.minute = newMinute;
}
}